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

烈酒焚心 提交于 2019-12-08 04:50:36

问题


I'm trying to convert a image file (a png, but could be anything) that I just extracted into memory from a compressed file to a ID2D1Bitmap to draw using Direct 2D. I tried to look for some documentation, but I can only find methods that receive "const char* path" or ask me width and height of my image, that I can't know before-hand. Searching on google for it got me nowhere.

The file is raw in memory, and I would like to avoid to extract images to the hdd into a temporary file just to read their data from there.

Any ideas?


回答1:


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



来源:https://stackoverflow.com/questions/26566849/how-to-convert-a-image-file-loaded-in-memory-to-a-id2d1bitmap-in-c

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