C++ WIC Direct2D CreateBitmapFromMemory:宽度和高度限制?
当宽度小于等于644时,CreateBitmapFromMemory将成功执行。 如果该值超过此值,则HRESULT值为-2003292276 宽度和高度是否有限制C++ WIC Direct2D CreateBitmapFromMemory:宽度和高度限制?,c++,direct2d,wic,C++,Direct2d,Wic,当宽度小于等于644时,CreateBitmapFromMemory将成功执行。 如果该值超过此值,则HRESULT值为-2003292276 宽度和高度是否有限制 #include <d2d1.h> #include <d2d1helper.h> #include <wincodecsdk.h> // Use this for WIC Direct2D functions void test() { IWICImagingFactory
#include <d2d1.h>
#include <d2d1helper.h>
#include <wincodecsdk.h> // Use this for WIC Direct2D functions
void test()
{
IWICImagingFactory *m_pIWICFactory;
ID2D1Factory *m_pD2DFactory;
IWICBitmap *m_pEmbeddedBitmap;
ID2D1Bitmap *m_pD2DBitmap;
unsigned char *pImageBuffer = new unsigned char[1024*1024];
HRESULT hr = S_OK;
int _nHeight = 300;
int _nWidth = 644;
是否确实为函数CreateBitmapFromMemory传递了正确的像素格式?您将其硬编码为GUID_WICPixelFormat24bppRGB,我认为这是根本原因,您应该确保此格式与从中复制数据的源位图的格式相同。尝试使用GetPixelFormat函数获取正确的格式,而不是硬代码。错误代码是,原因现在很明显了
第二个是高度。你把它们放错顺序了。总之,您提供的参数不正确,导致缓冲区损坏。GPU上的图像尺寸有一个上限 在渲染目标上调用GetMaximumBitmapSize。 得到的是垂直或水平方向的最大像素。
对于较大的图像,您必须将其加载到软件渲染目标(如位图渲染目标),然后从中渲染所需的内容。当CreateBitmapFromMemory返回时,您可以使用GetLastError获取准确的错误信息,hr=-2003292276,GetLastError()返回0,转换为十六进制:0x88982F8C,Direct2D错误代码以0x889开头,但此处未列出此错误。错误为E_INVALIDARG,但这没有帮助,我们不知道MS如何检查参数,但有一个解决方法,您可以将bitsPerPixel设置为32以使代码正常工作。bitsPerPixel在CreateBitmapFromMemory的参数中设置(,,GUID\u WICPixelFormat24bppRGB,…),是否必须在其他地方指定它?计算步幅=\u nWidth*3,因为24/8=3,请尝试使用32位颜色,即\u nWidth*(32/8)=\u nWidth*4。
//_nWidth = 648;
if (m_pIWICFactory == 0 )
{
hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
// Create WIC factory
hr = CoCreateInstance(
CLSID_WICImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&m_pIWICFactory)
);
if (SUCCEEDED(hr))
{
// Create D2D factory
hr = D2D1CreateFactory( D2D1_FACTORY_TYPE_SINGLE_THREADED, &m_pD2DFactory );
}
}
hr = m_pIWICFactory->CreateBitmapFromMemory(
_nHeight, // height
_nWidth, // width
GUID_WICPixelFormat24bppRGB, // pixel format of the NEW bitmap
_nWidth*3, // calculated from width and bpp information
1024*1024, // height x width
pImageBuffer, // name of the .c array
&m_pEmbeddedBitmap // pointer to pointer to whatever an IWICBitmap is.
);
if (!SUCCEEDED(hr)) {
char *buffer = "Error in CreateBitmapFromMemory\n";
}
}