+// New Create/FreeDIB functions since ones in dibutils.cpp are confusing
+static long createDIB(long xSize, long ySize, long bitsPerPixel,
+ HPALETTE hPal, LPBITMAPINFO* lpDIBHeader);
+static long freeDIB(LPBITMAPINFO lpDIBHeader);
+
+// Creates a bitmap that matches the device context, from
+// an arbitray bitmap. At present, the original bitmap must have an
+// associated palette. TODO: use a default palette if no palette exists.
+// Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
+wxBitmap wxBitmap::GetBitmapForDC(wxDC& dc) const
+{
+ wxMemoryDC memDC;
+ wxBitmap tmpBitmap(this->GetWidth(), this->GetHeight(), dc.GetDepth());
+ HPALETTE hPal = NULL;
+ LPBITMAPINFO lpDib;
+ void *lpBits = NULL;
+
+/*
+ wxASSERT( this->GetPalette() && this->GetPalette()->Ok() && (this->GetPalette()->GetHPALETTE() != 0) );
+
+ tmpBitmap.SetPalette(this->GetPalette());
+ memDC.SelectObject(tmpBitmap);
+ memDC.SetPalette(this->GetPalette());
+
+ hPal = (HPALETTE) this->GetPalette()->GetHPALETTE();
+*/
+ if( this->GetPalette() && this->GetPalette()->Ok() && (this->GetPalette()->GetHPALETTE() != 0) )
+ {
+ tmpBitmap.SetPalette(* this->GetPalette());
+ memDC.SelectObject(tmpBitmap);
+ memDC.SetPalette(* this->GetPalette());
+ hPal = (HPALETTE) this->GetPalette()->GetHPALETTE();
+ }
+ else
+ {
+ hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE);
+ wxPalette palette;
+ palette.SetHPALETTE( (WXHPALETTE)hPal );
+ tmpBitmap.SetPalette( palette );
+ memDC.SelectObject(tmpBitmap);
+ memDC.SetPalette( palette );
+ }
+
+ // set the height negative because in a DIB the order of the lines is reversed
+ createDIB(this->GetWidth(), -this->GetHeight(), this->GetDepth(), hPal, &lpDib);
+
+ lpBits = malloc(lpDib->bmiHeader.biSizeImage);
+
+ ::GetBitmapBits((HBITMAP)GetHBITMAP(), lpDib->bmiHeader.biSizeImage, lpBits);
+
+ ::SetDIBitsToDevice((HDC) memDC.GetHDC(), 0, 0, this->GetWidth(), this->GetHeight(),
+ 0, 0, 0, this->GetHeight(), lpBits, lpDib, DIB_RGB_COLORS);
+
+ free(lpBits);
+
+ freeDIB(lpDib);
+ return (tmpBitmap);
+}
+