作者:hja2045905 | 来源:互联网 | 2023-05-25 07:02
问题来源:
http://www.cnblogs.com/del/archive/2008/08/10/1071357.html#1282779
本例效果图:
![](https://img.php1.cn/3cd4a/189d8/978/7dbdf0f38ad53545.jpeg'; {要保存的图片路径}
var
str: string;
bit: TBitmap;
Page_edge: Integer; {页面边距变量}
Word_h,Line_h: Integer; {字体高度和行间距}
w,x,y: Integer; {文本宽度与输出位置}
i: Integer;
begin
str := Memo1.Text;
bit := TBitmap.Create;
bit.Canvas.Font.Name := ComboBox1.Text;
bit.Canvas.Font.Size := StrToIntDef(LabeledEdit1.Text, 9);
Page_edge := StrToIntDef(LabeledEdit2.Text, 0);
Word_h := bit.Canvas.TextHeight('a');
Line_h := StrToIntDef(LabeledEdit3.Text, 0);
{设置页面高度}
bit.Height := (Word_h + Line_h) * Memo1.Lines.Count - Line_h + Page_edge * 2;
w := 0;
x := Page_edge;
y := Page_edge;
for i := 0 to Memo1.Lines.Count - 1 do
begin
{设置页面宽度}
if bit.Canvas.TextWidth(Memo1.Lines[i]) > w then
begin
w := bit.Canvas.TextWidth(Memo1.Lines[i]);
bit.Width := w + Page_edge * 2;
end;
{画文本}
bit.Canvas.TextOut(x, y, Memo1.Lines[i]);
{下一行}
Inc(y, Word_h + Line_h);
end;
{保存为图片}
bit.SaveToFile(FilePath);
bit.Free;
end;
end.
<hr>
</pre> 窗体文件:
<hr>
<pre class=)
object Form1: TForm1
Left = 0
Top = 0
ActiveCOntrol= Memo1
Caption = #25226#25991#26412#20445#23384#20026#22270#29255
ClientHeight = 274
ClientWidth = 370
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate= FormCreate
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 242
Top = 7
Width = 24
Height = 13
Caption = #23383#20307
end
object Memo1: TMemo
Left = 0
Top = 0
Width = 234
Height = 274
Align = alLeft
Lines.Strings = (
'Memo1')
ScrollBars = ssBoth
TabOrder = 0
ExplicitHeight = 249
end
object LabeledEdit1: TLabeledEdit
Left = 240
Top = 76
Width = 121
Height = 21
EditLabel.Width = 24
EditLabel.Height = 13
EditLabel.Caption = #23383#21495
TabOrder = 1
end
object Button1: TButton
Left = 242
Top = 227
Width = 120
Height = 25
Caption = #25226#25991#26412#20445#23384#20026#22270#29255
TabOrder = 2
OnClick= Button1Click
end
object ComboBox1: TComboBox
Left = 240
Top = 25
Width = 120
Height = 21
ItemHeight = 13
TabOrder = 3
Text = 'ComboBox1'
end
object LabeledEdit2: TLabeledEdit
Left = 240
Top = 120
Width = 121
Height = 21
EditLabel.Width = 24
EditLabel.Height = 13
EditLabel.Caption = #36793#36317
TabOrder = 4
end
object LabeledEdit3: TLabeledEdit
Left = 240
Top = 168
Width = 121
Height = 21
EditLabel.Width = 24
EditLabel.Height = 13
EditLabel.Caption = #34892#36317
TabOrder = 5
end
end