471,309 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,309 software developers and data experts.

Converting RAW data to MetaFile

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
Dec 22 '06 #1
2 10044
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" <yo*@yop.comwrote in message
news:eH***************@TK2MSFTNGP03.phx.gbl...
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

Dec 22 '06 #2
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, Jr." <mp*********@nospam.jun0.c0ma écrit dans le
message de news: eI****************@TK2MSFTNGP03.phx.gbl...
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" <yo*@yop.comwrote in message
news:eH***************@TK2MSFTNGP03.phx.gbl...
> 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


Dec 27 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Julian Ziersch | last post: by
1 post views Thread by sendhil | last post: by
3 posts views Thread by Richard Skopal | last post: by
4 posts views Thread by mabosch | last post: by
reply views Thread by weiruic | last post: by
2 posts views Thread by NickP | last post: by
1 post views Thread by bern11 | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.