我需要在画布上绘制一个PNG (在TPicture中),有以下要求:
下面的代码完成了这一任务,但是使用了GDI+和:
BitBlt
绘制简单的非透明位图要慢得多。在快速处理器上,绘制时间从1ms增加到16 1ms。在一个缓慢的CPU上,它从100 it增加到900 it。这是GDI+代码。如果下列情况下,它将返回到标准BitBlt:
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
ComCtrls, ExtCtrls,
GDIPObj, GDIPAPI;
...
procedure DrawPictureToBitmap(Bitmap: TBitmap; X, Y: Integer; Picture: TPicture);
function PictureToGPBitmap(Picture: TPicture): TGPBitmap;
var
MemStream: TMemoryStream;
begin
MemStream := TMemoryStream.Create;
try
Picture.Graphic.SaveToStream(MemStream);
MemStream.Position := 0;
Result := TGPBitmap.Create(TStreamAdapter.Create(MemStream));
finally
FreeAndNil(MemStream);
end;
end;
var
GDICanvas: TGPGraphics;
GPImage: TGPImage;
begin
GDICanvas := TGPGraphics.Create(Bitmap.Canvas.Handle);
try
GPImage := PictureToGPBitmap(Picture);
try
GDICanvas.DrawImage(GPImage, X, Y);
// Did the draw succeed?
if GDICanvas.GetLastStatus <> Ok then
begin
// No, try a BitBlt!
BitBlt(Bitmap.Canvas.Handle, X, Y, Bitmap.Height, Bitmap.Width, Picture.Bitmap.Canvas.Handle, 0, 0, SRCCOPY);
end;
finally
FreeAndNil(GPImage);
end;
finally
FreeAndNil(GDICanvas);
end;
end;
更新1
使用David的建议,我使用了Delphi内置的PNG支持,成功地摆脱了GDI+。
procedure DrawPictureToBitmap(Bitmap: TBitmap; X, Y: Integer; Picture: TPicture);
var
PNG: TPngImage;
MemStream: TMemoryStream;
begin
PNG := TPngImage.Create;
try
MemStream := TMemoryStream.Create;
try
Picture.Graphic.SaveToStream(MemStream);
MemStream.Position := 0;
PNG.LoadFromStream(MemStream);
finally
FreeAndNil(MemStream);
end;
PNG.Draw(Bitmap.Canvas, Rect(X, Y, X + Picture.Width, Y + Picture.Height));
finally
FreeAndNil(PNG);
end;
end;
不幸的是,绘制时间与GDI+方法完全相同。有什么方法可以对此进行优化吗?
发布于 2013-08-02 02:05:49
在我看来,你是没有必要的采取内存中的图形,压缩到PNG,然后去压缩。你可以直接绘制图形。
只需在位图画布上调用Draw
,传递Picture.Graphic
procedure DrawPictureToBitmap(Bitmap: TBitmap; X, Y: Integer; Picture: TPicture);
begin
Bitmap.Canvas.Draw(X, Y, Picture.Graphic);
end;
这时,您可能会决定DrawPictureToBitmap
是没有意义的,删除它,并直接调用Bitmap.Canvas.Draw()
。
这也将有一个好的好处,您的图片并不局限于包含一个PNG图像,按照问题中的代码。
https://stackoverflow.com/questions/18013011
复制相似问题