create a bmp header at byte level
Posted on 2004-04-28
I'm relateively new to C# and very new to it's byte and stream methods so this task is beyond me.
What I'm trying to do is read image information out of a sql DB stored as byte data. The image information is raw dib. I'd like to convert this to bmp and post it to a form image box so i need at add bmp header information to it. This is where I'm lost. I've done this is PHP but I don't know how to convert it to C#. Here's the PHP code:
$info = unpack("lwidth/lhight/Splanes/Sbitcount/Lcompression/Lsizeimage/lxpelspermeter/lypelspermeter/Lclrused/lclrimportant",$av_image);
$offset = 54 + ($info['clrused'] * 4);
$header = pack("cclssL", ord('B'), ord('M'), strlen($av_image)+14, 0, 0, $offset, 40); // type(2), size, reserved, reserved, offset, header_size
So basically what I am doing is extracting the "clrused" value (unsigned long, 4 bytes) information from the dib and using the value to create "offset". Then creating the header (char, char, long, short shot, long).
This header is then added to a memory stream followed by the dib data.
This is what I have so far, but it's a mess and no where near complete:
byte data = (byte)myCommand.ExecuteScalar();
if(data.Length > 0)
// Open a stream for the image and write the bytes into it
System.IO.MemoryStream stream = new System.IO.MemoryStream(data, true);
stream.Write(data, 0, data.Length);
byte clrused = new byte;
stream.Read(clrused, 28, 4);
int offset = 54 + (Convert.ToInt32(clrused) * 4);
//stream.Write(data, 0, data.Length);
// Create a bitmap from the stream
Bitmap bmp = new Bitmap(stream);
// Close the stream
Thanks for the help!