Results 1 to 5 of 5

Thread: [RESOLVED] Problem storing a picturebox content to an array

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Resolved [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.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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
    Last edited by jmsrickland; Mar 18th, 2016 at 02:23 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  3. #3
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    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)
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Problem storing a picturebox content to an array

    Thank you guys!
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width