GDI和Delphi,PNG资源,DrawImage,ColorConversion – > Out of Memory

我已经开始在Delphi 2009中使用GDI了.我想做的事情是加载PNG资源并在将其绘制到Graphics对象时应用颜色转换.我正在使用
http://www.bilsen.com/gdiplus/中提供的代码.为此,我刚刚为TGPBitmap添加了一个新的构造函数,该构造函数使用了< www.codeproject.com> /KB/GDI-plus/cgdiplusbitmap.aspx(C)或 /board/index.php?topic = 10191.0(MASM)转换为Delphi.

作为参考,转换后的代码如下:

constructor TGPBitmap.Create(const Instance: HInst; const PngName: String; dummy : PngResource_t);
const
    cPngType : string = 'PNG';

var
    hResource : HRSRC;
    imageSize : DWORD;
    pResourceData : Pointer;
    hBuffer : HGLOBAL;
    pBuffer : Pointer;
    pStream : IStream;

begin
    inherited Create;

    hResource := FindResource(Instance, PWideChar(PngName), PWideChar(cPngType));
    if hResource = 0 then Exit;

    imageSize := SizeofResource(Instance, hResource);
    if imageSize = 0 then Exit;

    pResourceData := LockResource(LoadResource(Instance, hResource));
    if pResourceData = nil then Exit;

    hBuffer := GlobalAlloc(GMEM_MOVEABLE, imageSize);

    if hBuffer <> 0 then
    begin
        try
            pBuffer := GlobalLock(hBuffer);

            if pBuffer <> nil then
            begin
                try
                    CopyMemory(pBuffer, pResourceData, imageSize);

                    if CreateStreamOnHGlobal(hBuffer, FALSE, pStream) = S_OK then
                    begin
                        GdipCheck(GdipCreateBitmapFromStream(pStream, FNativeHandle));
                    end;
                finally
                    GlobalUnlock(hBuffer);
                    pStream := nil;
                end;
            end;
        finally
            GlobalFree(hBuffer);
        end;
    end;
end;

代码似乎工作正常,因为我能够毫无问题地绘制加载的图像.但是,如果我在绘制时尝试应用颜色转换,那么我会得到一个可爱的错误:( GDI错误)内存不足.

如果我从文件加载位图,或者如果我创建一个临时的,我绘制初始位图,然后使用临时,那么它的工作正常.

让我感到困惑的是,如果我从codeproject中获取C项目,添加相同的PNG作为资源并使用相同的颜色转换(换句话说,我在Delphi中以相同的顺序执行完全相同的操作并具有相同的功能发生在同一个DLL上的调用,然后就可以了.

C代码如下所示:

    const Gdiplus::ColorMatrix cTrMatrix = {
        {
            {1.0, 0.0, 0.0, 0.0, 0.0},
            {0.0, 1.0, 0.0, 0.0, 0.0},
            {0.0, 0.0, 1.0, 0.0, 0.0},
            {0.0, 0.0, 0.0, 0.5, 0.0},
            {0.0, 0.0, 0.0, 0.0, 1.0}
        }
    };

    Gdiplus::ImageAttributes imgAttrs;
    imgAttrs.SetColorMatrix(&cTrMatrix, Gdiplus::ColorMatrixFlagsDefault, Gdiplus::ColorAdjustTypeBitmap);
    graphics.DrawImage(*pBitmap, Gdiplus::Rect(0, 0, pBitmap->m_pBitmap->GetWidth(), pBitmap->m_pBitmap->GetHeight()), 0, 0, pBitmap->m_pBitmap->GetWidth(), pBitmap->m_pBitmap->GetHeight(), Gdiplus::UnitPixel, &imgAttrs);

Delphi的对应物是:

const
  cTrMatrix: TGPColorMatrix = (
    M: ((1.0, 0.0, 0.0, 0.0, 0.0),
        (0.0, 1.0, 0.0, 0.0, 0.0),
        (0.0, 0.0, 1.0, 0.0, 0.0),
        (0.0, 0.0, 0.0, 0.5, 0.0),
        (0.0, 0.0, 0.0, 0.0, 1.0)));

var
    lImgAttrTr : IGPImageAttributes;
    lBitmap : IGPBitmap;

begin
    // ...
    lImgAttrTr := TGPImageAttributes.Create;
    lImgAttrTr.SetColorMatrix(cTrMatrix, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap);

        aGraphics.DrawImage
        (
            lBitmap,
            TGPRect.Create
            (
                0, 0, lBitmap.Width, lBitmap.Height
            ),
            0, 0, lBitmap.Width, lBitmap.Height,
            UnitPixel,
            lImgAttrTr
        );

我完全不知道可能导致这个问题的原因,谷歌也没有任何帮助.任何想法,评论和解释都非常感谢.

最佳答案 我无法完全遵循您的代码,因此请考虑这是一个更一般的说法:如果源位图和目标位图都不是32bbp,则常常会出现颜色转换时出现内存不足错误.

点赞