Har en bild i en picturebox som jag sparar ner och läser sen in i en byte array men jag vill slippa att spara bilden för att få in den i en array Låter som du skall Använda API GetPixel från PictureBoxen. Du kan oxå använda GetDIBits API anropet: Får det tyvärr inte att funka Det framgick inte av ditt inlägg. Vilket format ska du spara bilden? Nej det är inte så konstigt,om du använder binaryWrite så Det går bra med bmp men hade varit bättre med gif men då behöver man väl andra komponenter? Har hittat en lösningPicturebox till byte array
hur ska jag göra?Sv: Picturebox till byte array
Då blir det en Long Array.Sv: Picturebox till byte array
<code>
Option Explicit
Private Const BI_RGB = 0&
Private Const DIB_RGB_COLORS = 0 ' color table in RGBs
Private Type BITMAP
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
End Type
Private Type BITMAPINFOHEADER '40 bytes
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
End Type
Private Type RGBQUAD
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End Type
Private Type BITMAPINFO
bmiHeader As BITMAPINFOHEADER
bmiColors As RGBQUAD
End Type
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
Private Function GetBitmapBits(Picture As IPicture) As Byte()
Dim BITMAP As BITMAP
Dim bi24BitInfo As BITMAPINFO
Dim Data() As Byte
Dim Width As Long
Dim Height As Long
Dim Padding As Long
GetObject Picture.handle, Len(BITMAP), BITMAP
Width = BITMAP.bmWidth
Height = BITMAP.bmHeight
With bi24BitInfo.bmiHeader
.biBitCount = 24
.biCompression = BI_RGB
.biPlanes = 1
.biSize = Len(bi24BitInfo.bmiHeader)
.biWidth = Width
.biHeight = Height
End With
Padding = (4 - (Width Mod 4)) Mod 4
ReDim Data(1 To (Width + Padding) * Height * 3) As Byte
GetDIBits Picture.CurDC, Picture.handle, 0, Height, Data(1), bi24BitInfo, DIB_RGB_COLORS
GetBitmapBits = Data
End Function
Private Sub Form_Load()
Dim Data() As Byte
Data = GetBitmapBits(Picture1.Image)
End Sub
</code>Sv: Picturebox till byte array
Arrayn ska jag sedan skicka till en asp sida där jag sänder den med
Response.BinaryWrite till klienten men det blir ingen bild av det ;(
Några förslag?Sv: Picturebox till byte array
Sv: Picturebox till byte array
måste den vända sig till en Forms eller PictureBoxs Hdc. Sv: Picturebox till byte array
Sv: Picturebox till byte array
<code>
Private Type BITMAP
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
End Type
Private Type BITMAPFILEHEADER
bfType As Integer
bfSize As Long
bfReserved1 As Integer
bfReserved2 As Integer
bfOffBits As Long
End Type
Private Type BITMAPINFOHEADER
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
End Type
Private Type BITMAPINFO
bmiHeader As BITMAPINFOHEADER
'bmiColors As RGBQUAD 'not needed since we are creating a 24 bit bitmap
End Type
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal XSrc As Long, ByVal YSrc As Long, ByVal dwRop As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
Private Declare Function GetObjectAPI Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Const BI_RGB = 0
Private Const DIB_RGB_COLORS = 0
Private Function GetBitmapBits(Picture As IPicture) As Byte()
Dim bmpNew As BITMAP 'new bitmap
Dim bytImage() As Byte 'counter image bytes
Dim bfhNew As BITMAPFILEHEADER 'file header for the new bitmap
Dim biNew As BITMAPINFO 'info for the new bitmap
Dim lngSize As Long 'size of the new bitmap's bits
Dim bytDIBits() As Byte 'the bitmap's bits
Dim bytTemp As Byte 'byte holder to fix the offset and filesize bytes
Call GetObjectAPI(Picture.Handle, Len(bmpNew), bmpNew)
'build the bitmap header
With biNew.bmiHeader
.biSize = Len(biNew.bmiHeader)
.biWidth = bmpNew.bmWidth
.biHeight = bmpNew.bmHeight
.biPlanes = 1
.biBitCount = 24
.biCompression = BI_RGB
End With
'get the number of bits in the new bitmap
lngSize& = biNew.bmiHeader.biWidth * 3
lngSize& = (((lngSize& + 3) / 4) * 4) * biNew.bmiHeader.biHeight
'resize the array to hold the bits of the bitmap
ReDim bytDIBits(lngSize& - 1)
'retrieve the bitmap's bits
Call GetDIBits(Picture.CurDC, Picture.Handle, 0, bmpNew.bmHeight, bytDIBits(0), biNew, DIB_RGB_COLORS)
'get the bitmap file header
With bfhNew
.bfType = 19778
.bfSize = Len(bfhNew) + Len(biNew) + lngSize&
.bfOffBits = Len(biNew.bmiHeader) + Len(bmpNew) - 10
.bfReserved1 = 0
.bfReserved2 = 0
End With
'resize the array to hold the bitmap's binary data
ReDim bytImage(Len(bfhNew) + Len(biNew) + lngSize&)
'add the bitmap file header
Call CopyMemory(bytImage(0), bfhNew, Len(bfhNew))
'add the bitmap info
Call CopyMemory(bytImage(Len(bfhNew)), biNew, Len(biNew))
'add the bitmap's bits
Call CopyMemory(bytImage(Len(bfhNew) + Len(biNew)), bytDIBits(0), lngSize&)
'when copied into the array, the hiword and loword of the offset and filesize
'are reversed and need to be switched
bytTemp = (bytImage(2))
bytImage(2) = bytImage(4)
bytImage(4) = bytTemp
bytTemp = (bytImage(3))
bytImage(3) = bytImage(5)
bytImage(5) = bytTemp
bytTemp = (bytImage(10))
bytImage(10) = bytImage(12)
bytImage(12) = bytTemp
bytTemp = (bytImage(11))
bytImage(11) = bytImage(13)
bytImage(13) = bytTemp
'return the bytes
GetBitmapBits = bytImage
End Function
</code>