ImageEn, unit iexRichEdit

TIERichEdit.PaintTo

TIERichEdit.PaintTo


Declaration

procedure PaintTo(DestDC: HDC;
                  R: TRect;
                  HAlign: TIEHAlign = iehLeft;
                  VAlign: TIEVAlign = ievTop;
                  ScaleX: Double = 1; ScaleY: Double = 1;
                  AllowWrapping: Boolean = True); overload;
procedure PaintTo(Bitmap: TBitmap;
                  R: TRect;
                  HAlign: TIEHAlign = iehLeft;
                  VAlign: TIEVAlign = ievTop;
                  const PaintBackground: Boolean = False;
                  ScaleX: Double = 1; ScaleY: Double = 1;
                  AllowWrapping: Boolean = True); overload;
procedure PaintTo(Bitmap: TBitmap;
                  Margin: Integer = 0;
                  HAlign: TIEHAlign = iehLeft;
                  VAlign: TIEVAlign = ievTop;
                  const PaintBackground: Boolean = False;
                  ScaleX: Double = 1; ScaleY: Double = 1;
                  AllowWrapping: Boolean = True); overload;
procedure PaintTo(Bitmap: TIEBitmap;
                  R: TRect;
                  HAlign: TIEHAlign = iehLeft;
                  VAlign: TIEVAlign = ievTop;
                  const PaintBackground: Boolean = True;
                  const EnableAlpha: Boolean = False;
                  ScaleX: Double = 1; ScaleY: Double = 1;
                  AllowWrapping: Boolean = True); overload;
procedure PaintTo(Bitmap: TIEBitmap;
                  Margin: Integer = 0;
                  HAlign: TIEHAlign = iehLeft;
                  VAlign: TIEVAlign = ievTop;
                  const PaintBackground: Boolean = True;
                  const EnableAlpha: Boolean = False;
                  ScaleX: Double = 1; ScaleY: Double = 1;
                  AllowWrapping: Boolean = True); overload;


Description

Draw the content to a TIEBitmap or TBitmap.
HAlign and VAlign specify the alignment of the text on the image.
If PaintBackground is true, the editor background is painted to the image (i.e. the image will have the same solid color background as the Editor, typically white). If false, the existing background (non-text area) of the image will be preserved.
If EnableAlpha is true, the background (non-text area) will be be transparent.
The Scale properties allow you to zoom the content (i.e. make text appear bigger or smaller).
Set AllowWrapping to false to force text to be drawn on a single line regardless of fit.


Examples

// Paint rich editor content to TImageEnView
const
  Text_Margin = 30;
var
  bmp : TIEBitmap;
  imgW, imgH, textW, textH: Integer;
  textRect: TRect;
begin
  imgW  := ImageEnView1.Width;
  imgH  := ImageEnView1.Height;
  textW := imgW - 2 * Text_Margin;

  // Calc height of text at this width
  textH := IERichEdit1.TextHeight( 0, textW );

  textRect := Rect( Text_Margin,
                    Text_Margin,
                    Text_Margin + textW,
                    Text_Margin + textH );

  bmp := TIEBitmap.Create;
  try
    bmp.width  := imgW;
    bmp.height := imgH;

    // Load an image to use as background?
    if not ( chkUseEditorBackground.checked or chkEnableAlpha.checked ) then
      bmp.LoadFromFile('D:\TextBackground.jpg');

    IERichEdit1.PaintTo( bmp, textRect, iehLeft, ievTop, chkUseEditorBackground.checked, chkEnableAlpha.checked );

    ImageEnView1.Assign(bmp);
  finally
    bmp.Free;
  end;

// Paint rich editor content to TImage
const
  Text_Margin = 30;
var
  bmp: TBitmap;
  imgW, imgH, textW, textH: Integer;
  textRect: TRect;
begin
  imgW  := Image1.Width;
  imgH  := Image1.Height;
  textW := imgW - 2 * Text_Margin;

  // Calc height of text at this width
  textH := IERichEdit1.TextHeight( 0, textW );

  textRect := Rect( Text_Margin,
                    Text_Margin,
                    Text_Margin + textW,
                    Text_Margin + textH );

  bmp := TBitmap.Create;
  try
    bmp.width  := imgW;
    bmp.height := imgH;

    // Load an image to use as background?
    if not chkUseEditorBackground.checked then
      bmp.LoadFromFile('D:\TextBackground.bmp');

    IERichEdit1.PaintTo( bmp, textRect, iehLeft, ievTop, chkUseEditorBackground.checked );

    Image1.Picture.Assign(bmp);
  finally
    bmp.Free;
  end;
end;

// Output an RTF file as an image
const
  Out_Width   = 500;
  Out_Height  = 800;
  Text_Margin = 20;
var
  re : TIERichEdit;
  bmp: TIEBitmap;
  textRect: TRect;
begin
  bmp := TIEBitmap.Create;
  re := TIERichEdit.CreateParented(HWND(HWND_MESSAGE));
  try
    re.Visible  := False;
    re.WordWrap := False;
    re.OpenFromFile( 'D:\RTF Document.rtf' );

    bmp.width  := Out_Width;
    bmp.height := Out_Height;

    textRect := Rect( Text_Margin,
                      Text_Margin,
                      Out_Width - Text_Margin,
                      Out_Height - Text_Margin );

    re.PaintTo( bmp, textRect, iehCenter, ievCenter, True );

    bmp.SaveToFile('D:\RtfImage.jpeg' );
  finally
    bmp.Free;
    re.Free;
  end;
end;