}// GetManResource //------------------------------------------------- // TranslateBitmapToBeCompatibleWithScreen // // This is really messy. A quick explanation of what's going on.... // // If we were to call Bitmap.GetHbitmap, we'll get back an HBitmap that // is intended for 32-bit displays. // // MMC expects the HBITMAP it gets to be compatible with the screen. So, // if the current video display is not set to 32 bit, MMC will fail to show the bitmap. // // So what do we do? We translate the bitmaps into the color depth that the screen // is currently displaying //------------------------------------------------- internal static IntPtr TranslateBitmapToBeCompatibleWithScreen(IntPtr hBitmap) { IntPtr hFinalHBitmap = IntPtr.Zero; // Get a DC that is compatible with the display IntPtr hdc = CreateCompatibleDC(IntPtr.Zero); // Now get the BITMAP information on this guy DIBSECTION ds = new DIBSECTION(); int nLen = GetObject(hBitmap, Marshal.SizeOf(typeof(DIBSECTION)), ref ds); // Create a BITMAPINFO structure and put in the appropriate values BITMAPINFO bmi = new BITMAPINFO(); bmi.bmiHeader = ds.dsBmih; // Now, we want to change the color depth from 32 bpp to whatever the screen supports... int nOldDepth = bmi.bmiHeader.biBitCount; bmi.bmiHeader.biBitCount = (ushort)GetDeviceCaps(hdc, 12 /* BITSPIXEL */); IntPtr bits; // Garbage variable... just need it for the function call // This will create a bitmap handle with the color depth of the current screen hFinalHBitmap = CreateDIBSection(hdc, ref bmi, 0 /* DIB_RGB_COLORS */, out bits, IntPtr.Zero, 0); // Put back the bitmap's original color depth bmi.bmiHeader.biBitCount = (ushort)nOldDepth; // Translate the 32bpp pixels to something the screen can show SetDIBits(hdc, hFinalHBitmap, 0, bmi.bmiHeader.biHeight, ds.dsBm.bmBits, ref bmi, 0); // Cleanup DeleteDC(hdc); return(hFinalHBitmap); }// TranslateBitmapToBeCompatibleWithScreen
internal static extern int GetObject(IntPtr hgdiobj, int cbBuffer, ref DIBSECTION lpvObject);