Converting RAW data to MetaFile

L

Laurent Navarro

Hello,


I am using a library which returns a byte[] containing RAW data, ie all
pixels' color values coded in a byte array without header. I would like to
save those data into a JPEG file so I tried to use the MetaFile class.

byte[] data;
(...) // Creating the RAW image through the DLL call.
MemoryStream memoryStream = new MemoryStream(data);
MetaFile metaFile = new MetaFile(memoryStream);

Unfortunately, this code throws an exception which description is: "GDI+
encounters an internal error". I also tried this:

byte[] data;
(...) // Creating the RAW image through the DLL call.
MemoryStream memoryStream = new MemoryStream(data);
Image image = Image.FromStream(memoryStream);

This time I have an ArgumentException. I guess I can't use a stream
linking to RAW data as it is not an image with header. But then, how can I
make the MetaFile class read my data and convert them to JPEG file ???

Thank you !


Laurent
 
M

Michael Phillips, Jr.

If you wish to save your raw data as a .jpeg file, you need to create a
bitmap from the raw bytes and then have the gdiplus encoder create a .jpeg
file for you.

1) Using your memory stream, create and populate in order:
BITMAPFILEHEADER
BITMAPINFOHEADER
palette(i.e., if raw data is indexed)
RAW bytes
2) You now have a correctly formatted bitmap that the gdiplus decoder will
create a bitmap from your memory stream.
3) Use the save method to save your bitmap as a .jpeg encoded file.
 
L

Laurent Navarro

Hi Michel,


Thank you for your answer. I tried and it worked, but I had a bad
surprise: the GDI+ works in BGR and not RGB. So before saving my datas I had
to swap the R and B value for each pixel of my data.

Is there an easier way to make the Bitmap class work in RGB without
converting the image first ?

Thanks :)



Michael Phillips said:
If you wish to save your raw data as a .jpeg file, you need to create a
bitmap from the raw bytes and then have the gdiplus encoder create a .jpeg
file for you.

1) Using your memory stream, create and populate in order:
BITMAPFILEHEADER
BITMAPINFOHEADER
palette(i.e., if raw data is indexed)
RAW bytes
2) You now have a correctly formatted bitmap that the gdiplus decoder will
create a bitmap from your memory stream.
3) Use the save method to save your bitmap as a .jpeg encoded file.


Laurent Navarro said:
Hello,


I am using a library which returns a byte[] containing RAW data, ie
all pixels' color values coded in a byte array without header. I would
like to save those data into a JPEG file so I tried to use the MetaFile
class.

byte[] data;
(...) // Creating the RAW image through the DLL call.
MemoryStream memoryStream = new MemoryStream(data);
MetaFile metaFile = new MetaFile(memoryStream);

Unfortunately, this code throws an exception which description is:
"GDI+ encounters an internal error". I also tried this:

byte[] data;
(...) // Creating the RAW image through the DLL call.
MemoryStream memoryStream = new MemoryStream(data);
Image image = Image.FromStream(memoryStream);

This time I have an ArgumentException. I guess I can't use a stream
linking to RAW data as it is not an image with header. But then, how can
I make the MetaFile class read my data and convert them to JPEG file ???

Thank you !


Laurent
 

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