uses
Windows, Classes, Graphics, ComCtrls, RichEdit;
procedure DrawRTF(DestDCHandle: HDC;
const R: TRect;
RichEdit: TRichEdit; PixelsPerInch: Integer);
var
TwipsPerPixel: Integer;
Range: TFormatRange;
begin
TwipsPerPixel := 1440
div PixelsPerInch;
Range.hDC := DestDCHandle;
{DC handle}
Range.hdcTarget := DestDCHandle;
{ditto}
{Convert the coordinates to twips (1/1440")}
Range.rc.Left := R.Left * TwipsPerPixel;
Range.rc.Top := R.Top * TwipsPerPixel;
Range.rc.Right := R.Right * TwipsPerPixel;
Range.rc.Bottom := R.Bottom * TwipsPerPixel;
Range.rcPage := Range.rc;
{Start at character zero}
Range.chrg.cpMin := 0;
{Display all Characters}
Range.chrg.cpMax := -1;
{RichEdit.GetTextLen;}
{Free cached information}
RichEdit.Perform(EM_FORMATRANGE, 0, 0);
{First measure the text, to find out how high the format rectangle will be.
The call sets fmtrange.rc.bottom to the actual height required, if all
characters in the selected range will fit into a smaller rectangle}
RichEdit.Perform(EM_FORMATRANGE, 0, DWord(@Range));
{Now render the text}
RichEdit.Perform(EM_FORMATRANGE, 1, DWord(@Range));
{Free cached information}
RichEdit.Perform(EM_FORMATRANGE, 0, 0);
end;
procedure RTFToBmp(MainWND : HWND;AFileName :
string;
var ABitMap : TBitmap);
var
RichEdit: TRichEdit;
DestDCHandle: HDC;
begin
RichEdit :=TRichEdit.CreateParented(MainWND);
try
RichEdit.Width := GetDeviceCaps(GetDC(RichEdit.Handle),VERTRES);
RichEdit.Height := GetDeviceCaps(GetDC(RichEdit.Handle),HORZRES);
RichEdit.Visible := False;
RichEdit.Lines.LoadFromFile(AFileName);
ABitMap.width := RichEdit.Width;
ABitMap.height := RichEdit.Height;
DestDCHandle := ABitMap.Canvas.Handle;
DrawRTF(DestDCHandle, Rect(0, 0, ABitMap.Width, ABitMap.Height), RichEdit, 96);
finally
RichEdit.Free;
end;
end;