FMX.Graphics.TBitmap如何轉換爲VCL.Graphics.TBitmap或Vcl.Imaging.PngImage.TPngImage?如何將FMX.Graphics.TBitmap轉換爲VCL.Graphics.TBitmap或Vcl.Imaging.PngImage.TPngImage?
我在我的項目中同時具有FMX格式和VCL格式。
FMX.Graphics.TBitmap如何轉換爲VCL.Graphics.TBitmap或Vcl.Imaging.PngImage.TPngImage?如何將FMX.Graphics.TBitmap轉換爲VCL.Graphics.TBitmap或Vcl.Imaging.PngImage.TPngImage?
我在我的項目中同時具有FMX格式和VCL格式。
感謝David Heffernan和一些搜索我寫了這些函數如下。
我先拿出不支持阿爾法
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,它支持阿爾法。
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;
這是非常緩慢的天真解決方案。看看https://delphi-foundations.googlecode.com/svn/trunk/XE2%20book/13.%20Native%20APIs/Taking%20a%20screenshot/ScreenshotForm.pas –
校正與矩形圖像的工作:
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(j,i);
Result.Canvas.Pixels[j,i]:=
RGB(
TAlphaColorRec(AlphaColor).R,
TAlphaColorRec(AlphaColor).G,
TAlphaColorRec(AlphaColor).B);
end;
end;
finally
b.Unmap(data);
end;
end;
請用英文。 –
我想,你可以保存到內存流,然後加載。但是使用Map來獲取位圖數據可能更有效率,然後以這種方式填充新的VCL位圖。 –