FireMonkey在使用GDIPlus放大图像的Bug的修复

Delphi10.2中FireMonkey中GDIPlus放大图片的Bug,不知道从哪个版本开始的,还是一直都这样。

新建一个FireMonkey工程,窗口上放一个Image控件,WrapMode设置为Stretch,表示随控件大小缩放。指定一张小图。

默认使用D2D的时候放大没问题

关闭D2D使用GDIPlus渲染

initialization
GlobalUseDirect2D := False;
finalization

end.

再看:

 

边缘多了一圈渐变。

找到FMX.Canvas.GDIP.pas文件,拷贝到我们工程所在目录中。作如下修改


procedure TCanvasGdiPlus.DoDrawBitmap(const ABitmap: TBitmap;
const SrcRect, DstRect: TRectF; const AOpacity: Single;
const HighSpeed: Boolean);
var
CM: TColorMatrix;
ImageAttributes: TGPImageAttributes;
GPBitmap: TGPBitmap;
M: TBitmapData;
begin
if HighSpeed then
FGPGraphics.SetInterpolationMode(InterpolationModeNearestNeighbor)
else
FGPGraphics.SetInterpolationMode(InterpolationModeHighQuality);

if (FPrinter <> nil) and not TCanvasManager.DefaultCanvas.InheritsFrom
(TCanvasGdiPlus) then
begin
// In printer
if ABitmap.Map(TMapAccess.Read, M) then
try
GPBitmap := TGPBitmap.Create(ABitmap.Width, ABitmap.Height, M.Pitch,
PixelFormat32bppPARGB, M.Data);
try
if AOpacity < 1 then
begin
if SameValue(AOpacity, 0, TEpsilon.Vector) then
Exit;
if not ABitmap.HandleAllocated then
Exit;
CM := ImageColorMatrix;
CM[3][3] := AOpacity;
ImageAttributes := TGPImageAttributes.Create;
try
ImageAttributes.SetColorMatrix(CM, ColorMatrixFlagsDefault,
ColorAdjustTypeBitmap);
FGPGraphics.DrawImage(GPBitmap,
MakeRect(DstRect.Left, DstRect.Top,
DstRect.Right - DstRect.Left, DstRect.Bottom - DstRect.Top),
SrcRect.Left, SrcRect.Top, SrcRect.Right - SrcRect.Left,
SrcRect.Bottom - SrcRect.Top, UnitPixel, ImageAttributes);
finally
ImageAttributes.Free;
end;
end
else
begin
if not ABitmap.HandleAllocated then
Exit;
FGPGraphics.DrawImage(GPBitmap, MakeRect(DstRect.Left, DstRect.Top,
DstRect.Right - DstRect.Left, DstRect.Bottom - DstRect.Top),
SrcRect.Left, SrcRect.Top, SrcRect.Right - SrcRect.Left,
SrcRect.Bottom - SrcRect.Top, UnitPixel);
end;
finally
GPBitmap.Free;
end;
finally
ABitmap.Unmap(M);
end;
Exit;
end;

if AOpacity < 1 then
begin
if SameValue(AOpacity, 0, TEpsilon.Vector) then
Exit;
if not ABitmap.HandleAllocated then
Exit;
CM := ImageColorMatrix;
CM[3][3] := AOpacity;
ImageAttributes := TGPImageAttributes.Create;
try
ImageAttributes.SetColorMatrix(CM, ColorMatrixFlagsDefault,
ColorAdjustTypeBitmap);
FGPGraphics.DrawImage(TGPBitmap(ABitmap.Handle),
MakeRect(DstRect.Left, DstRect.Top, DstRect.Right - DstRect.Left,
DstRect.Bottom - DstRect.Top), SrcRect.Left, SrcRect.Top,
SrcRect.Right - SrcRect.Left, SrcRect.Bottom - SrcRect.Top, UnitPixel,
ImageAttributes);
finally
ImageAttributes.Free;
end;
end
else
begin
if not ABitmap.HandleAllocated then
Exit;
ImageAttributes := TGPImageAttributes.Create;
try
ImageAttributes.SetWrapMode(WrapModeTileFlipXY);
FGPGraphics.DrawImage(TGPBitmap(ABitmap.Handle),
MakeRect(DstRect.Left, DstRect.Top, DstRect.Right - DstRect.Left,
DstRect.Bottom - DstRect.Top), SrcRect.Left, SrcRect.Top,
SrcRect.Right - SrcRect.Left, SrcRect.Bottom - SrcRect.Top, UnitPixel, ImageAttributes);
finally
ImageAttributes.Free;
end;
end;
end;

关键点在于ImageAttributes.SetWrapMode(WrapModeTileFlipXY);

此条目发表在Delphi, 未分类分类目录,贴了, , , , 标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

你必须启用JavaScript