如何从在C ++ / CLI一个RenderTargetBitmap的位图结构? [英] How to get an BITMAP struct from a RenderTargetBitmap in C++/CLI?

查看:181
本文介绍了如何从在C ++ / CLI一个RenderTargetBitmap的位图结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF RenderTargetBitmap在C ++ / CLI,我希望能够从它创建位图结构与BitBlt函数使用。我没有与位图或RenderTargetBitmap多以前,所以任何想法将是巨大的!

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!

推荐答案

原来,这是一个有点比单纯使用CopyPixels更复杂。

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

在C ++ / CLI管理code,我做到以下几点:

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;

然后我就可以调用,我想填补了的CBitmap从非托管code路过该例程。那么我可以用它作为我想任何原生的CBitmap。

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.

这篇关于如何从在C ++ / CLI一个RenderTargetBitmap的位图结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆