Author: Tomas Rutkauskas
Does anyone know of a component or a few lines of code, that could turn the
contents of a TRichText field (WIN32 RTF) into a bitmap?
Answer:
Add this in the unit your are developing:
1 uses
2 RichText;
3
4 {For this demo add a RichEdit and an Image Control set the RichEdit change event to
5 the lower code}
6
7 procedure
8 OutputRTFToBmp(RichHolder: TRichEdit; ImageHolder: TBitmap; itemwidth,
9 itemheight: real);
10 var
11 Range: TFormatRange;
12 TextBoundary: TRect;
13 begin
14 {Setup the Height and Width of our output}
15 ImageHolder.width := round(itemwidth * screen.PixelsPerInch);
16 ImageHolder.height := round(itemheight * screen.PixelsPerInch);
17 {Set the Size of the Rich Edit}
18 textboundary := rect(0, 0, round(itemwidth * 1440), round(itemheight * 1440));
19 {Set the Range record}
20 range.hdc := ImageHolder.Canvas.handle;
21 range.hdctarget := ImageHolder.Canvas.handle;
22 range.rc := textboundary;
23 range.rcpage := textboundary;
24 {Start at character zero}
25 range.chrg.cpMin := 0;
26 {Display all Characters}
27 range.chrg.cpMax := -1;
28 {Ask RTF to Draw}
29 Sendmessage(RichHolder.handle, EM_FORMATRANGE, 1, longint(@range));
30 {Cleanup RTF Cache}
31 sendmessage(RichHolder.handle, EM_FORMATRANGE, 0, 0);
32 end;
33
34 procedure TForm1.RichEdit1Change(Sender: TObject);
35 begin
36 OutputRTFToBmp(RichEdit1, Image1.picture.bitmap, 2, 2);
37 {Display new stuff, this will flicker so you will have to double buffer}
38 image1.refresh;
39 end;
I use it on Metafiles then you can scale it also.
|