WIC Direct2D CreateBitmapFromMemory:宽度和高度的限制

WIC Direct2D CreateBitmapFromMemory: limitations on width and height?

本文关键字:高度 Direct2D CreateBitmapFromMemory WIC      更新时间:2023-10-16

CreateBitmapFromMemory在_nWidth等于或小于644时成功执行。如果该值超过此值,则 HRESULT 值为 -2003292276

宽度和高度是否存在限制?

#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;

如果 nWidth 超过 644,CreateBitmapFromMemory 将返回错误。

    //_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 CreateBitmapFromMemoryn";
    }
}

错误代码0x88982F8C WINCODEC_ERR_INSUFFICIENTBUFFER,原因现在很明显了?

第一个参数是宽度,第二个参数是高度。你把它们按错误的顺序排列。总而言之,您提供了不正确的参数,导致缓冲区错误。

您确定为函数 CreateBitmapFromMemory 传递了正确的 pixelFormat 吗? 您将其硬编码为GUID_WICPixelFormat24bppRGB,我认为这是根本原因,您应该确保此格式与您从中复制数据的源位图的格式相同。 尝试使用 GetPixelFormat 函数来获取正确的格式,而不是硬代码。

GPU 上图像的尺寸有上限。

在呈现器目标上调用 GetMaximumBitmapSize。http://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=EN-US&k=k(GetMaximumBitmapSize);k(DevLang-C%2B%2B);k(TargetOS-Windows)&rd=true

你得到的是垂直或 horiz 的最大像素。对于较大的图像,您必须将它们加载到软件呈现目标(如位图呈现目标)中,然后从中渲染您想要的内容。