How to convert a image file loaded in memory to a

2019-06-01 16:53发布

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条回答
乱世女痞
2楼-- · 2019-06-01 17:38

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

查看更多
登录 后发表回答