问题来源: http://www.cnblogs.com/del/archive/2008/08/10/1071357.html#1282779

本例效果图:



代码文件:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;               {用于输入要保存的文本}
    ComboBox1: TComboBox;       {字体}
    LabeledEdit1: TLabeledEdit; {字号}
    LabeledEdit2: TLabeledEdit; {页面边距}
    LabeledEdit3: TLabeledEdit; {行距}
    Button1: TButton;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{初始化数据}
procedure TForm1.FormCreate(Sender: TObject);
begin
  ComboBox1.Items := Screen.Fonts;
  ComboBox1.Text := '宋体';
  LabeledEdit1.Text := '12';
  LabeledEdit2.Text := '10';
  LabeledEdit3.Text := '4';
end;

procedure TForm1.Button1Click(Sender: TObject);
const
  FilePath = 'c:\temp\Word.bmp'; {要保存的图片路径}
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.

窗体文件:
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

posted on 2008-08-11 14:03  万一  阅读(3473)  评论(2编辑  收藏  举报