本文介绍了如何在C ++中将内存中加载的图像文件转换为ID2D1Bitmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将刚从压缩文件提取到内存中的图像文件(png,但是可以是任何东西)转换为ID2D1Bitmap,以使用Direct 2D进行绘制.我试图寻找一些文档,但是我只能找到接收"const char * path"或询问我图像宽度和高度的方法,这些是我事先不知道的.在Google上搜索它无处可寻.

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.

文件是内存中的原始文件,我想避免将图像提取到HDD到一个临时文件中,只是为了从那里读取它们的数据.

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.

有什么想法吗?

推荐答案

如果您具有HBITMAP句柄,则可以执行以下操作:

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

  1. 使用以下代码的图片大小: :: GetObject(hBmp,sizeof(BITMAP),& bmpSizeInfo);
  2. 像这样填写BITMAPINFO: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;

创建足够的堆内存来保存位图的数据:pBuff = new char [bmpSizeInfo.bmWidth * bmpSizeInfo.bmHeight * 4];

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

像这样获取位图数据::: GetDIBits(hDc,hBmp,0,bmpSizeInfo.bmHeight,(void *)pBuff,& bmpData,DIB_RGB_COLORS);

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

创建一个 D2D1_BITMAP_PROPERTIES 并像这样填充它:bmpPorp.dpiX = 0.0f;bmpPorp.dpiY = 0.0f;bmpPorp.pixelFormat.format = DXGI_FORMAT_B8G8R8A8_UNORM;bmpPorp.pixelFormat.alphaMode = D2D1_ALPHA_MODE_IGNORE;

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;

使用渲染目标将数据转换为ID2D1BitmappRT-> CreateBitmap(bmpSize,pBuff,4 * bmpSizeInfo.bmWidth,bmpPorp和& pBmpFromH);

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

希望这会有所帮助.

山姆

这篇关于如何在C ++中将内存中加载的图像文件转换为ID2D1Bitmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-13 04:49