我写了一个简单的例子:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtDlgs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Image1: TImage;
Button1: TButton;
OpenPictureDialog1: TOpenPictureDialog;
Button2: TButton;
Button3: TButton;
SaveDialog1: TSaveDialog;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenPictureDialog1.Execute then
Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if Assigned(Image1.Picture.MetaFile) then //这句有错,后面更正
begin
with TMetafileCanvas.Create(Image1.Picture.MetaFile, 0) do
try
Draw(0,0, Image1.Picture.MetaFile);
Font.Name := 'Arial Black';
Font.Size := 14;
TextOut(80,50,'This is a Sample Text to MetaFile');
Brush.Color := clBlue;
Ellipse(100, 100, 200, 200);
finally
Free;
end;
end
else
ShowMessage('The Image is not a MetaFile');
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
if SaveDialog1.Execute then
Image1.Picture.SaveToFile(SaveDialog1.FileName);
end;
end.
花了十几分钟,希望有帮助。
判断是否为METAFILE那一句有错,应该是:
if Assigned(Image1.Picture.Graphic) and (Image1.Picture.Graphic is TMetaFile) then