RGB into a picturebox

R

Ringo

I'm using RoboRealm to get an image from a webcam. the roborealm docs
say to do something like
byte[] image = new byte[d.width * d.height];
Dimension dd = rr.getImage(image, d.width * d.height);

to get the image. the docs call the image an "RGB triplet". how can I
display this in a picturebox?

Small wods please, I'm a HW guy :)

Ringo
 
P

Peter Duniho

I'm using RoboRealm to get an image from a webcam. the roborealm docs
say to do something like
byte[] image = new byte[d.width * d.height];
Dimension dd = rr.getImage(image, d.width * d.height);

to get the image. the docs call the image an "RGB triplet". how can I
display this in a picturebox?

To display in a picture box control, you need a Bitmap instance. You can
use the LockBit() and UnlockBits() methods to get access to the underlying
data for the bitmap (LockBits()), copy the byte array you've retrieved to
the underlying data, and then signal that you're done with the data
(UnlockBits()).

Note that while the example code in the MSDN documentation does not appear
to concern itself with the Stride value of the BitmapData class, it's not
at all clear to me that's correct. Typically, you'd have to use the
Stride value to calculate the start of each new row of bitmap data,
because internally bitmaps have rows of data rounded in length rounded up
to ensure that each row is on an aligned memory address (usually aligned
on 16-bit boundaries for regular Windows bitmaps).

In other words, rather than just copying the entire "image" array to the
Scan0 address in the BitmapData class (as demonstrated in the sample code
on MSDN), you really should only copy one row of data at a time,
calculating a new destination address by adding Stride to the previous
row's starting address (starting with Scan0 of course).

Finally, you need to make sure you're using the right kind of bitmap
data. It sounds as though your RoboRealm library is returning 24-bit
image data, and so as long as you create a Bitmap with that format, you
should be able to essentially copy the data directly.

All that said, I haven't played with the .NET version of this at all, so
you may find that it's actually providing more automatic functionality
than I am assuming. For example, there's a possibility that you could
just use the LockBits() method that includes a BitmapData instance as
input with the ImageLockMode flags set to include UserInputBuffer. As
long as you properly describe your input data (24-bits, Scan0 pointing to
an unsafe version of your array, Stride set to the actual length between
rows of your array), it may be that would just work directly without you
having to copy data. I'm not sure.

The above is the basic idea. If the specifics aren't exactly right, it
should at least get you pointed in the right direction.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top