2009. március 8., vasárnap

How to convert the content of a TRichEdit into a bitmap


Problem/Question/Abstract:

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:


uses
  RichText;

{For this demo add a RichEdit and an Image Control set the RichEdit change event to the lower code}

procedure
  OutputRTFToBmp(RichHolder: TRichEdit; ImageHolder: TBitmap; itemwidth, itemheight: real);
var
  Range: TFormatRange;
  TextBoundary: TRect;
begin
  {Setup the Height and Width of our output}
  ImageHolder.width := round(itemwidth * screen.PixelsPerInch);
  ImageHolder.height := round(itemheight * screen.PixelsPerInch);
  {Set the Size of the Rich Edit}
  textboundary := rect(0, 0, round(itemwidth * 1440), round(itemheight * 1440));
  {Set the Range record}
  range.hdc := ImageHolder.Canvas.handle;
  range.hdctarget := ImageHolder.Canvas.handle;
  range.rc := textboundary;
  range.rcpage := textboundary;
  {Start at character zero}
  range.chrg.cpMin := 0;
  {Display all Characters}
  range.chrg.cpMax := -1;
  {Ask RTF to Draw}
  Sendmessage(RichHolder.handle, EM_FORMATRANGE, 1, longint(@range));
  {Cleanup RTF Cache}
  sendmessage(RichHolder.handle, EM_FORMATRANGE, 0, 0);
end;

procedure TForm1.RichEdit1Change(Sender: TObject);
begin
  OutputRTFToBmp(RichEdit1, Image1.picture.bitmap, 2, 2);
  {Display new stuff, this will flicker so you will have to double buffer}
  image1.refresh;
end;


I use it on Metafiles then you can scale it also.

Nincsenek megjegyzések:

Megjegyzés küldése