我使用TSVGImageIcon來讀取SVG矢量影像,我創建了這個例程來將影像保存為一定大小的位圖 :
function SVG2Bitmap(Imatge: TBytes; x, y: integer): TBytes;
var SVG: TSVGIconImage;
流。TBytesStream。
Bitmap。TBitmap。
Resultat。TBytesStream。
表格。TForm;
begin[/span
trynil;
Form := nil;
Stream := nil;
Bitmap := nil;
Resultat := nil;
Stream := TBytesStream.Create(Imatge);
Stream.Position := 0;
Form := TForm.Create(nil); // SVGIconImage如果不在Form里面會引起錯誤。
SVG := TSVGIconImage.Create(Form);
SVG.Parent := Form;
SVG.Stretch := True;
SVG.Proportional := True;
SVG.LoadFromStream(Stream);
SVG.Width := x;
SVG.Height := y;
Bitmap := TBitmap.Create;
Bitmap.SetSize(x, y);
SVG.PaintTo(Bitmap.Canvas, 0, 0) 。
Resultat := TBytesStream.Create。
Bitmap.SaveToStream(Resultat);
Result := Resultat.Bytes.Create; Bitmap.SaveToStream(Resultat); Resultat := Resultat.Bytes.Create
最終
if Assigned(SVG) then try SVG.Free except end 。
if Assigned(Form) then try Form.Free except end;
if Assigned(Bitmap) then try Bitmap.Free except end;
if Assigned(Stream) then try Stream.Free except end;
if Assigned(Resultat) then try Resultat.Freeexcept end;
end;
end;
它作業得很好,但它將透明區域填充為灰色,我希望它們是白色的。你能推薦一種方法,在設定透明度顏色的同時將SVG匯出為位圖,或者我應該回圈瀏覽位圖,將灰色像素改為白色?
謝謝你。
uj5u.com熱心網友回復:
事實證明,SVGIconImage使用父容器的顏色作為透明度顏色。所以將Form.Color改為我選擇的TransparencyColor就可以了。
function SVG_2_Bitmap(ImageSVG。TBytes; x, y: integer; TransparencyColor: TColor = clWhite): TBytes;
var SVG: TSVGIconImage;
Stream, Resultat: TBytesStream。
Bitmap。TBitmap。
表格。TForm。
begin[/span
SVG := nil;
Form := nil;
Stream := nil;
Bitmap := nil;
Resultat := nil;
trynil)。
Form.Color := TransparencyColor;
SVG := TSVGIconImage.Create(Form);
SVG.Parent := Form;
SVG.Proportional := True;
SVG.LoadFromStream(Stream);
SVG.Width := x;
SVG.Height := y;
Bitmap := TBitmap.Create;
Bitmap.SetSize(x, y);
SVG.PaintTo(Bitmap.Canvas, 0, 0) 。
Resultat := TBytesStream.Create。
Bitmap.SaveToStream(Resultat);
Result := Resultat.Bytes.Create; Bitmap.SaveToStream(Resultat); Resultat := Resultat.Bytes.Create
最終
try SVG.Free except end;
try Form.Free except end;
try Bitmap.Free except end;
try Stream.Free except end;
try Resultat.Free except end;
end;
end;
感謝您的所有建議。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/316487.html
標籤:
