开发者

How to get an BITMAP struct from a RenderTargetBitmap in C++/CLI?

开发者 https://www.devze.com 2022-12-26 07:52 出处:网络
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 tho开

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 tho开发者_JS百科ughts would be great!


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消