Results 1 to 8 of 8

Thread: Image to array

  1. #1

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

    Image to array

    I want to copy the image in a picturebox to a 1D array of integers where each elment would be a pixel colour.

    I'm wondering whether it's best to use .Net methods which partly I have yet to learn or rather stick to the API functions I was using in VB6, such as in this example provided to me by a forum user:

    Code:
    Public Function RetDIBDataLong(PictureObject As Object, Optional Return2DArray As Boolean) As Long()
    Dim BM As BITMAP, bmi As BITMAPINFO, lngImageData() As Long
      'The PictureObject must be an object with both a HDC and Image property
      'such as a PictureBox, UserControl or Form
    
        Dim hDC As Long, bReleaseDC As Boolean, hHandle As Long
        
        If TypeOf PictureObject Is StdPicture 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
    At any rate, I've been using .Net only for a short time and I still get lost in the new graphics methods. I need something like,

    Dim imgArray as Integer() = SomeFunction(PictureBox1.Image)
    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Image to array

    If you were to use GetPixel in a loop then it would be very slow, so that's probably not the way to go. I'm not sure whether what you have got there would be the best option in VB.NET or not. I think that there might be a thread in the CodeBank that deals with reading pixel colours quickly.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Addicted Member
    Join Date
    Nov 2011
    Posts
    223

    Re: Image to array

    there might be a thread in the CodeBank that deals with reading pixel colours quickly.
    Probably the most and the best examples of that type come from contributor boop boops , his examples use the InteropServices.Marshal.LockBits( ) method.

    Search for lockbits and it pulls up quite a few posts.

  4. #4
    Addicted Member
    Join Date
    Nov 2011
    Posts
    223

  5. #5
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Image to array

    If you do this in C# there are numerous examples, because it has access to "unsafe code" which is a scary way to say "it can access pointers".

    If you do this in VB, your best bet is to use the example Mc_VB posted. It does as close to what C# does as it can, with an extra copy of the pixel data that is a penalty VB cannot avoid.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  6. #6

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

    Re: Image to array

    Quote Originally Posted by Mc_VB View Post
    Thanks, this looks very much like what I was after, I'm going to start playing around with it.
    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)

  7. #7

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

    Re: Image to array

    Quote Originally Posted by Sitten Spynne View Post
    If you do this in C# there are numerous examples, because it has access to "unsafe code" which is a scary way to say "it can access pointers".

    If you do this in VB, your best bet is to use the example Mc_VB posted. It does as close to what C# does as it can, with an extra copy of the pixel data that is a penalty VB cannot avoid.
    Thanks for the suggestion but my knowledge of C# is still inaproppriate to say the least.
    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)

  8. #8

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

    Thumbs up Re: Image to array

    Quote Originally Posted by Mc_VB View Post
    I got it to work in no time. Very easy to use and fast as lightning!
    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