将RichEdit的内容输出到image中

下面示例可以将RichEdit的内容输出到一个Image中,只要你将
Image设为足够大,即可得到RichEdit中的所有内容,然后再
Image1.Picture.SaveToFile输出到一个BMP文件即可:

uses RichEdit;

procedure TForm1.Button1Click(Sender: TObject);
var
  Range: TFormatRange;
  LastChar, MaxLen, LogX, LogY: Integer;
begin
  FillChar(Range, SizeOf(TFormatRange), 0);
  with Image1, Range do
  begin
    LogX := GetDeviceCaps(Canvas.Handle, LOGPIXELSX);
    LogY := GetDeviceCaps(Canvas.Handle, LOGPIXELSY);
    hdc := Canvas.Handle;
    hdcTarget := hdc;
    rc.right := Image1.ClientWidth * 1440 div LogX;
    rc.bottom := Image1.ClientHeight * 1440 div LogY;
    rcPage := rc;
    LastChar := 0;
    MaxLen := GetTextLen;
    chrg.cpMax := -1;
    repeat
      chrg.cpMin := LastChar;
      LastChar := SendMessage(RichEdit1.Handle, EM_FORMATRANGE, 1,
                              Longint(@Range));
    until (LastChar >= MaxLen) or (LastChar = -1);
  end;
  SendMessage(RichEdit1.Handle, EM_FORMATRANGE, 0, 0);
  Image1.Refresh;
end;

你可能感兴趣的:(image)