|
-
Feb 16th, 2018, 06:43 AM
#1
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)
-
Feb 16th, 2018, 07:45 AM
#2
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.
-
Feb 16th, 2018, 10:14 AM
#3
Addicted Member
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.
-
Feb 16th, 2018, 10:18 AM
#4
Addicted Member
-
Feb 16th, 2018, 11:31 AM
#5
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.
-
Feb 19th, 2018, 04:47 AM
#6
Re: Image to array
 Originally Posted by Mc_VB
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)
-
Feb 19th, 2018, 04:50 AM
#7
Re: Image to array
 Originally Posted by Sitten Spynne
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)
-
Feb 19th, 2018, 05:48 AM
#8
Re: Image to array
 Originally Posted by Mc_VB
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|