如何将FMX.Graphics.TBitmap转换为VCL.Graphics.TBitmap或Vcl.Imaging.PngImage.TPngImage?
我的项目中同时包含FMX表单和VCL表单。
多亏了大卫·赫弗南(David Heffernan)和一些搜索,我将这些函数编写如下。
我首先想出了不支持Alpha的功能
function ConvertFmxBitmapToVclBitmap(b:FMX.Graphics.TBitmap):Vcl.Graphics.TBitmap;
var
data:FMX.Graphics.TBitmapData;
i,j:Integer;
AlphaColor:TAlphaColor;
begin
Result:=VCL.Graphics.TBitmap.Create;
Result.SetSize(b.Width,b.Height);
if(b.Map(TMapAccess.Readwrite,data))then
try
for i := 0 to data.Height-1 do begin
for j := 0 to data.Width-1 do begin
AlphaColor:=data.GetPixel(i,j);
Result.Canvas.Pixels[i,j]:=
RGB(
TAlphaColorRec(AlphaColor).R,
TAlphaColorRec(AlphaColor).G,
TAlphaColorRec(AlphaColor).B);
end;
end;
finally
b.Unmap(data);
end;
end;
我编写了第二个函数,将FMX.Graphics.TBitmap转换为Vcl.Imaging.PngImage.TPngImage,它支持Alpha。
function ConvertFmxBitmapToPng(b:FMX.Graphics.TBitmap):Vcl.Imaging.PngImage.TPngImage;
var
data:FMX.Graphics.TBitmapData;
i,j:Integer;
AlphaColor:TAlphaColor;
AlphaLine:VCL.Imaging.PngImage.pByteArray;
begin
result:=TPngImage.CreateBlank(COLOR_RGBALPHA, 8, b.Width, b.Height);;
if(b.Map(TMapAccess.Readwrite,data))then
try
for i := 0 to data.Height-1 do begin
AlphaLine:=Result.AlphaScanline[i];
for j := 0 to data.Width-1 do begin
AlphaColor:=data.GetPixel(j,i);
AlphaLine^[j]:=TAlphaColorRec(AlphaColor).A;
Result.Pixels[j,i]:=
RGB(
TAlphaColorRec(AlphaColor).R,
TAlphaColorRec(AlphaColor).G,
TAlphaColorRec(AlphaColor).B);
end;
end;
finally
b.Unmap(data);
end;
end;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句