繁体   English   中英

GDI +和Delphi,PNG资源,DrawImage,ColorConversion->内存不足

[英]GDI+ & Delphi, PNG resource, 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 ++)或<www.masm32.com> / 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 +错误)内存不足。

如果我从文件中加载位图,或者创建了一个临时图,将初始位图绘制到该临时图上,然后使用该临时图,那么它就可以正常工作。

, add the same PNG as resource and use the same color conversion (in other words, do the exact same thing I am doing in Delphi in the same order and with the same function calls that happen to go to the same DLL), then it works. 让我感到烦恼的是,如果我从获取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
        );

对于可能导致此问题的原因,我一无所知,而Google并没有任何帮助。 任何想法,评论和解释都受到高度赞赏。

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

我有同样的问题。 从资源加载的图像的像素格式实际上不是PixelFormat32bppARGB。 应该是,但似乎不是。 我有效的解决方案是:

// Create local bimap with PixelFormat32bppARGB flag
Gdiplus::Bitmap local_bmp(WIDTH, HEIGHT, PixelFormat32bppARGB);
Gdiplus::Graphics gfx(&local_bmp);

// Redraw the original bitmap on the local without transformation.
gfx.DrawImage(&resource_bmp, 0, 0);

// ... set the matrix and attributes

// Do the transformation on the local_bmp
screenGfx.DrawImage(&local_bmp,Rect(0, 0, WIDTH, HEIGHT), 0, 0, WIDTH, HEIGHT, Gdiplus::UnitPixel, &imgAttrs);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM