go to:

Monday, November 24, 2008

DIB, C#, and Me...

Ok, after pulling out my hair trying to generate thumbnails from device-independent bitmaps stored by a CArchive in an "old" MFC program, I finally got help here at the workplace on this.

I must say I could not find this easily with much Googling, ergo I'm gonna post this up here with comments for anyone else out there looking for the same thing.

Please suggest any updations to comments and / or code, if you see the need...

NOTE: This code has been updated. Look here for it.

// our BITMAPINFOHEADER struct, as per gdi
// use LayoutKind to make sure data is marshalled as we've laid it out
public uint biSize;
public int biWidth;
public int biHeight;
public ushort biPlanes;
public ushort biBitCount;
public uint biCompression;
public uint biSizeImage;
public int biXPelsPerMeter;
public int biYPelsPerMeter;
public uint biClrUsed;
public uint biClrImportant;
public void Init()
biSize = (uint)Marshal.SizeOf(this);

public static Bitmap BitmapFromDIB(MemoryStream dib)
// get byte array of device independent bitmap
byte[] dibBytes = dib.ToArray();

// get the handle for the byte array and "pin" that memory (i.e. prevent garbage collector from
// gobbling it up right away)...
GCHandle hdl = GCHandle.Alloc(dibBytes, GCHandleType.Pinned);

// marshal our data into a BITMAPINFOHEADER struct per Win32 definition of BITMAPINFOHEADER
BITMAPINFOHEADER dibHdr = (BITMAPINFOHEADER)Marshal.PtrToStructure(hdl.AddrOfPinnedObject(), typeof(BITMAPINFOHEADER));

// go ahead and release the "pin" from our handle on that memory

// If the target device does not have one plane, or we're working with a bitmap other than a
// non-compressed (BI_RGB) bitmap, we're not gonna work woith it
if (dibHdr.biPlanes != 1 || dibHdr.biCompression != 0)
return null;

// we need to know beforehand the pixel-depth of our bitmap
PixelFormat fmt = PixelFormat.Format24bppRgb;
switch (dibHdr.biBitCount)
case 32:
fmt = PixelFormat.Format32bppRgb;
case 24:
fmt = PixelFormat.Format24bppRgb;
case 16:
fmt = PixelFormat.Format16bppRgb555;
return null;

// prepare for our output bitmap
Bitmap bmp = new Bitmap(dibHdr.biWidth, dibHdr.biHeight, fmt);

// load our "empty" bitmap into memory and lock it for writing in the format we specified
BitmapData bd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, fmt);

// marshal our device independent bitmap data over to our output bitmap
Marshal.Copy(dibBytes, Marshal.SizeOf(dibHdr), bd.Scan0, bd.Stride * bd.Height);

// we're done marshalling, so release our bitmapdata lock

// DIB data is upside-down for some reason, so flip it

// return our bitmap
return bmp;


Submit this story to DotNetKicks

Tuesday, November 11, 2008

C++, Web Services, TinyXML, and Me...


So my current employer wants our existing MFC application to interface with some web services exposed by our new website (sorry, need to stay hush-hush about it at the moment).  Now, while the MFC side of things is turning out to be a bitch, I threw together some classes to make life easier for me.  The source is here.

CppHttp.h is a simple class to do GET and POST requests through.  Currently, it does NOT do multi-part requests, but I promise to update this when I get to that.  To consume the responses, I'm using TinyXML.  I looked at some other libraries, including using the MsXml stuff, but they were WAY overboard what I needed.  I included a sample class that calls a stock market thing I found on  The Find method in there is particularly useful when getting, say, a DataTable back and wanting to go straight to the nodes.

Anyway, hope this is useful.  Let me know your thoughts.


Submit this story to DotNetKicks