byte[,,] to Bitmap?

M

mallolnolla

Hello there,

I'm trying to display a grabbed picture from a matrox meteor II to a
picturebox (I know I can use the matrox display, but I need to show it
on a PictureBox). I've tried to fill the bitmap with the setpixel
method, but it's too much slow (my camera's resolution is about 700x500
pixels).

Does anybody know how to fill the bitmap from a 3-dimensional array
(width x height x R/G/B), or even 3 2-dimensional arrays?

Any help would be greeted.

I've been searching for information through the internet, but I didn't
find the appropiate solution.

Thanks a lot
 
B

Bob Powell [MVP]

I would suggest creating a bitmap of the same size and using LockBits to
enable you to iterate the three dimensional array and transfer that info to
the individual bytes of the bitmap.

See the GDI+ FAQ for detailed info on LockBits.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
N

Nicholas Paldino [.NET/C# MVP]

Even better, if your environment allows, use unsafe code. The three
dimensional array that you have should be one contiguous block in memory,
and you can cast that to a byte*, and copy the elements using CopyMemory (by
just passing pointers).

Hope this helps.
 
M

mallolnolla

Thank you all for your quick reply. I've tried the LockBit option
(using unsafe code anyway), but I've another problem:
to set a pixel value, I don't have enough space using a byte, because
I've three bytes for every pixel (R, G and B). How do I set a pixel
value on the bitmap then? Using one byte per pixel only allows me to
have a grayscale picture, and I need a color one.

Thank you again
 
M

mallolnolla

I've readed MSDN help and thought a little ;) I think I've almost
solved the problem

Thanx a lot
 
B

Bob Powell [MVP]

You have obviously misunderstood the way LockBits works. Take a look at my
explanation and pay attention to the different layouts for the different
pixel formats. You will see that the full pixel depth is catered for.

It's unlikely your array will have the required stride so it will only be by
coincidence that you can copy the memory to the BitmapData buffer directly
as Nick suggests.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 

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