views:

167

answers:

2

I've got a WPF RenderTargetBitmap in C++/CLI and I want to be able to create a BITMAP structure from it to use with BitBlt. I've not worked with BITMAP or RenderTargetBitmap much before, so any thoughts would be great!

+1  A: 

How 'bout BitmapSource.CopyPixels together with SetDIBits? After the usual CreateCompatibleBitmap / CreateCompatibleDC / SelectObject incantations, of course.

Ben Voigt
Thanks, Ben. I'll try this out.
Mike Caron
A: 

Turns out that it was a little more complicated than simply using CopyPixels.

In the C++/CLI managed code, I do the following:

virtual BOOL fillBitmap(CDC* dc, CBitmap* bmp)
{   
  WPFContainer^ page = getWPFContainer();

  // Get a bitmap from the DVE page
  RenderTargetBitmap ^rtb = gcnew RenderTargetBitmap(page->ActualWidth, page->ActualHeight, 96, 96, PixelFormats::Default);
  rtb->Render(page);

  // fill up the passed in bitmap with the bits from WPF
  BITMAPINFO bmi;
  ZeroMemory(&bmi, sizeof(BITMAPINFO));
  bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  bmi.bmiHeader.biWidth = rtb->PixelWidth;
  bmi.bmiHeader.biHeight = -rtb->PixelHeight;   /* Use a Negative height so that we copy the bits from TOP to BOTTOM with CopyPixels below */
  bmi.bmiHeader.biPlanes = 1;
  bmi.bmiHeader.biBitCount = 32;
  bmi.bmiHeader.biCompression = BI_RGB;
  bmi.bmiHeader.biSizeImage = rtb->PixelWidth * rtb->PixelHeight * 4;

  void* pBmpPixels;
  bmp->Attach(CreateDIBSection(*dc, &bmi, DIB_RGB_COLORS, &pBmpPixels, NULL, 0));

  if (NULL != pBmpPixels)
  {
    Int32Rect rTemp(0,0,0,0);
    IntPtr ipTemp(pBmpPixels);
    rtb->CopyPixels(rTemp, ipTemp, bmi.bmiHeader.biSizeImage, rtb->PixelWidth * 4);
  }

  return TRUE;
} override;

I can then call that routine from unmanaged code passing in the CBitmap that I want filled up. I can then use it as I would any native CBitmap.

Mike Caron