在加载的图像上写文字,是不是很神奇啊!其实做起来还是很简单的,主要是用到了画布类,在图像上写文字罢了!
这里我们要用到四个按钮,2个对话框,一个编辑框和一个图像框。
设置的效果如下图:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm1 = class(TForm) ImgPanel: TImage; Panel1: TPanel; TBOpenBMP: TButton; EDWrite: TEdit; TBClose: TButton; TBSave: TButton; TBStartWrite: TButton; OpenD: TOpenDialog; SaveD: TSaveDialog; procedure TBOpenBMPClick(Sender: TObject); procedure TBStartWriteClick(Sender: TObject); procedure TBSaveClick(Sender: TObject); procedure TBCloseClick(Sender: TObject); procedure ImgPanelMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure EDWriteKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure FormCreate(Sender: TObject); private { Private declarations } MX,MY:Integer; start:Boolean; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} //打开对话框操作 procedure TForm1.TBOpenBMPClick(Sender: TObject); begin with OpenD do begin if Execute then begin imgpanel.Picture.LoadFromFile(FileName); TBStartWrite.Enabled:=True; end; end; end; //单击写入文字操作 procedure TForm1.TBStartWriteClick(Sender: TObject); begin start:= True; TButton(Sender).Enabled:=False; end; //保存图像 procedure TForm1.TBSaveClick(Sender: TObject); begin with SaveD do begin if Execute then begin ImgPanel.Picture.SaveToFile(FileName); end; end; end; procedure TForm1.TBCloseClick(Sender: TObject); begin Close; end; //在图像上按下鼠标的操作 procedure TForm1.ImgPanelMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin MX:=x; MY:=y; if start then begin with EDWrite do begin Parent:=Form1; show; left:=MX; Top:=MY; SetFocus; end; end; end; procedure TForm1.EDWriteKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if(Key = vk_return)then begin if Trim(TEdit(Sender).Text)<>''then begin ImgPanel.Picture.Bitmap.Canvas.Font.Size:=20; //设置字体大小 ImgPanel.Picture.Bitmap.Canvas.Brush.Style:=bsClear; //画刷的类型,这里为空心 ImgPanel.Picture.Bitmap.Canvas.TextOut(MX,MY,TEdit(Sender).Text); //要输出的文字 end; start:=False; TEdit(Sender).Hide; //编辑框隐藏起来 TEdit(Sender).Clear; //清空 TBStartWrite.Enabled:=True; end; end; procedure TForm1.FormCreate(Sender: TObject); begin EDWrite.Hide; //编辑框隐藏起来 end; end.
运行结果如图: