[RESOLVED] Problem storing a picturebox content to an array
I have 2 picture boxes,
Picture1 is visible
Picture2 is invisible
I load a picture to Picture2 from a jpg file:
Picture2.Picture = LoadPicture(SomeFile)
and then I copy a small area from Picture2 to Picture1 and magnify it using StretchBlt:
StretchBlt Picture1.hDC, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, Picture2.hDC, zoomLeft, zoomTop, zoomWidth, zoomHeight, vbSrcCopy
Finally, for the purpose of manipulationg this image I use the function below -which I copied from this forums- to store it in a 2D array:
Dim arr2d() As Long
arr2d = RetDIBDataLong(Picture1, True)
where the function is:
Code:
Public Function RetDIBDataLong(PictureObject As Object, Optional Return2DArray As Boolean) As Long()
Dim BM As BITMAP, bmi As BITMAPINFO, lngImageData() As Long
Dim hDC As Long, bReleaseDC As Boolean, hHandle As Long
'The PictureObject must be an object with both a HDC and Image property
'such as a PictureBox, UserControl or Form
If TypeName(PictureObject) = "Picture" Then
hHandle = PictureObject.Handle
bReleaseDC = True
Else
hHandle = PictureObject.Picture
hDC = PictureObject.hDC
End If
If GetObjectA(hHandle, 24, BM) = 0& Then Exit Function
bmi.bmHeader.bmWidth = BM.bmWidth
bmi.bmHeader.bmHeight = BM.bmHeight
bmi.bmHeader.bmSize = 40
bmi.bmHeader.bmPlanes = 1
bmi.bmHeader.bmBitCount = 32
If bReleaseDC Then hDC = GetDC(GetDesktopWindow)
If Return2DArray Then
ReDim lngImageData(BM.bmWidth - 1, BM.bmHeight - 1)
GetDIBits hDC, hHandle, 0, BM.bmHeight, lngImageData(0, 0), bmi, 0
Else
ReDim lngImageData(BM.bmWidth * BM.bmHeight - 1)
GetDIBits hDC, hHandle, 0, BM.bmHeight, lngImageData(0), bmi, 0
End If
If bReleaseDC Then ReleaseDC GetDesktopWindow, hDC
RetDIBDataLong = lngImageData
End Function
But in the red line a 0 value is returned for hHandle and the function exits at the GetObjectA statememnt.
However, and this is what I don't understand, the function call works if I try it for test with Picture2.
Re: Problem storing a picturebox content to an array
When you do this:
StretchBlt Picture1.hDC, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, Picture2.hDC, zoomLeft, zoomTop, zoomWidth, zoomHeight, vbSrcCopy
you are not copying the picture from Picture2.Picture to Picture1.Picture but rather it becomes an image in Picture1.Image so you need to do this:
Set Picture1.Picture = Picture1.Image before you call RetDIBDataLong
Re: Problem storing a picturebox content to an array
I haven't tested this, but I believe this should also work:
Code:
arr2d = RetDIBDataLong(Picture1.Image, True)
Re: Problem storing a picturebox content to an array
Yes, that would work and the only reason I didn't mention it was because I felt it wouldn't teach him to learn to set .picture to .image because someday he wont have the option the change the function code
Re: Problem storing a picturebox content to an array