From raw data (byte[]) to Bitmap and vice versa?

G

Guest

I have 2 questions that are just the opposite to one another:

(1) I need to read an image file (like bitmap, jpeg etc.) and to save only
its data, I need to save his data in a raw data format, meaning; I don't want
to save the image header, only the pixels data.
the raw data should be saved in a byte array.
The direct way is to do a loop over the image pixels and to copy pixel by
pixel to the byte array.
I'm looking for another way to do that. Is there a .NET function/method that
does it?

The following code is not good because it saves also the bitmap header to
the byte array:
Bitmap img = Bitmap.FromFile(@"C:\WINDOWS\Blue Lace 16.bmp", true) as Bitmap;
MemoryStream ms = new MemoryStream();
img.Save(ms, img.RawFormat);
byte[] rawData = ms.ToArray();

(1) I have a raw data image in a byte array, and I need to build a bitmap
from it.
The raw data does not contain the image format (width and height), but I do
have it in other variables (int width; int height).

How do I do that?
 
O

Octavio Hernandez

Sharon,

Take a look at the Bitmap.LockBits() method, may be what you need.

Regards - Octavio
 

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