How to convert a image file loaded in memory to a ID2D1Bitmap in C++

懵懂的女人 提交于 2019-12-06 16:36:23

if you have the HBITMAP handle, you can do this:

  1. The the size of your image using: ::GetObject(hBmp, sizeof(BITMAP), &bmpSizeInfo);
  2. fill a BITMAPINFO like this: memset(&bmpData, 0, sizeof(BITMAPINFO)); bmpData.bmiHeader.biSize = sizeof(bmpData.bmiHeader); bmpData.bmiHeader.biHeight = -bmpSizeInfo.bmHeight; bmpData.bmiHeader.biWidth = bmpSizeInfo.bmWidth; bmpData.bmiHeader.biPlanes = bmpSizeInfo.bmPlanes; bmpData.bmiHeader.biBitCount = bmpSizeInfo.bmBitsPixel;

  3. create enough heap memory to hold the data for your bitmap: pBuff = new char[bmpSizeInfo.bmWidth * bmpSizeInfo.bmHeight * 4];

  4. Get the bitmap data like this: ::GetDIBits(hDc, hBmp, 0, bmpSizeInfo.bmHeight, (void*)pBuff, &bmpData, DIB_RGB_COLORS);

  5. Create a D2D1_BITMAP_PROPERTIES and fill it like this: bmpPorp.dpiX = 0.0f; bmpPorp.dpiY = 0.0f; bmpPorp.pixelFormat.format = DXGI_FORMAT_B8G8R8A8_UNORM; bmpPorp.pixelFormat.alphaMode = D2D1_ALPHA_MODE_IGNORE;

  6. Using your render target turn the data into ID2D1Bitmap pRT->CreateBitmap(bmpSize, pBuff, 4 * bmpSizeInfo.bmWidth, bmpPorp, &pBmpFromH);

Hope this can help.

Sam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!