Author: Lou Adler
How to create a *.jpg image from a TRichEdit
Answer:
1 uses
2 RichEdit;
3
4 procedure RichEditToJPEG(const aRichEdit: TRichEdit; const aJPEGImage: TJPEGImage);
5 var
6 lBMP: TBitmap;
7 liStyle: Integer;
8 liExStyle: Integer;
9 lR: TRect;
10 liTwipsPerPixel: Integer;
11 lfrFormatRange: TFormatRange; { Defined in RichEdit }
12 begin
13 lBMP := TBitmap.Create;
14 try
15 lBMP.Height := aRichEdit.Height;
16 lBMP.Width := aRichEdit.Width;
17 { Paint the richedit control's border }
18 aRichEdit.PaintTo(lBMP.Canvas.Handle, 0, 0);
19 { Store the richedit's window styles }
20 liStyle := GetWindowLong(aRichEdit.Handle, GWL_STYLE);
21 liExStyle := GetWindowLong(aRichEdit.Handle, GWL_EXSTYLE);
22 { Get canvas rect and adjust for the richedit's border if necessary }
23 lR := lBMP.Canvas.ClipRect;
24 if ((liStyle and WS_BORDER) <> 0) or ((liExStyle and WS_EX_CLIENTEDGE) <> 0)
25 then
26 begin
27 Inc(lR.Left, GetSystemMetrics(SM_CXEDGE));
28 Inc(lR.Top, GetSystemMetrics(SM_CYEDGE));
29 Dec(lR.Right, lR.Left);
30 Dec(lR.Bottom, lR.Top);
31 end;
32 { Adjust richedit's border by another pixel }
33 InflateRect(lR, -1, -1);
34 { We need twips to calculate sizes }
35 liTwipsPerPixel := 1400 div Screen.PixelsPerInch;
36 { Fill the TFormatRange record }
37 with lfrFormatRange do
38 begin
39 hdc := lBMP.Canvas.Handle;
40 hdcTarget := lBMP.Canvas.Handle;
41 { Convert the coordinates to twips }
42 rc := Rect(lR.Left * liTwipsPerPixel, lR.Top * liTwipsPerPixel,
43 lR.Right * liTwipsPerPixel, lR.Bottom * liTwipsPerPixel);
44 rcPage := rc;
45 chrg.cpMin := 0;
46 chrg.cpMax := -1;
47 end;
48 aRichEdit.Perform(EM_FORMATRANGE, 0, 0);
49 aRichEdit.Perform(EM_FORMATRANGE, 0, DWORD(@lfrFormatRange));
50 aRichEdit.Perform(EM_FORMATRANGE, 1, DWORD(@lfrFormatRange));
51 aRichEdit.Perform(EM_FORMATRANGE, 0, 0);
52 aJPEGImage.Assign(lBMP);
53 finally
54 lBMP.Free;
55 end;
56 end;
|