Bitmap generating C code

jdy0803

Member
Joined
May 20, 2013
Messages
17
Programming Experience
10+
I have a C code which create bitmap like this.


long SaveBmpFile( LPCTSTR filename, BYTE *ImageBitmap, DWORD BitmapByteCount, long width, long height)
{
FILE *fp;
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
DWORD dwBitmapByteCountPerLine;
DWORD dwBitmapByteCount;


dwBitmapByteCountPerLine = (width * 3 + 3) & (~3);
// Windows Bitmap requires each line to be 32bits boundary
dwBitmapByteCount = height * dwBitmapByteCountPerLine;


bfh.bfType = 0x4d42;
bfh.bfSize = BitmapByteCount + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);


bih.biSize = sizeof (BITMAPINFOHEADER);
bih.biWidth = width;
bih.biHeight = height;
bih.biPlanes = 1;
bih.biBitCount = 24;
bih.biCompression = BI_RGB;
bih.biSizeImage = 0;
bih.biXPelsPerMeter = 0;
bih.biYPelsPerMeter = 0;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
fp = fopen (filename, "wb");
if (! fp)
{
return -1;
}
// make Bitmap data
BYTE *bp = ImageBitmap;
fwrite (&bfh.bfType, 1, sizeof(BITMAPFILEHEADER), fp);
fwrite (&bih.biSize, 1, sizeof(BITMAPINFOHEADER), fp);
fwrite (bp, 1, dwBitmapByteCount, fp);
fclose (fp);
return 0;
}
Is there BITMAPFILEHEADER equivalent class of C#? or define by myself?
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,806
Location
Sydney, Australia
Programming Experience
10+
That type would be defined as part of the Windows API. Even when using the Windows API in C#, you have to define managed versions of all the unmanaged types and functions you want to use. In short, define it yourself.
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,184
Location
Norway
Programming Experience
10+
Bitmap class has a constructor that accepts a pointer to unmanaged image bytes.
 

jdy0803

Member
Joined
May 20, 2013
Messages
17
Programming Experience
10+
Yes I was lazy, I'll try web search.

I declared Structure.
Next I should write to file.
C# equivalent of fwrite is BinaryWriter? or BinaryFormatter?
I'm trying like this.
using (BinaryWriter writer = new BinaryWriter(File.Open(filename, FileMode.Create)))
{
writer.Write( ??? );
writer.Write( ??? );
}

But I don't know how to pass structure.
In C#, structure is value type, not reference.
How could I pass the pointer writer.Write method?
 
Last edited:
Top Bottom