Board index » delphi » Richtext in a bitmap

Richtext in a bitmap


2003-11-06 01:13:20 PM
delphi88
I have (among others) two fields in a database.
CUE is a memo field into which I have stored richtext. I want to store that
text into a bitmap and save the bitmap in a second graphic field called
BMPVIDEO, and I'd like BMPVIDEO to display bold, italic, etc.
The mechanics all work fine, except that BMPVIDEO contains the richtext
formatting characters. I create the bitmap with
Canvas.TextOut(20, 20, FieldByName('CUE').AsString);
I write BMPVIDEO with a SaveToStream.
How can I get formatted text from richtext into a bitmap?
 
 

Re:Richtext in a bitmap

Hi,
use EM_FORMATRANGE & EM_DISPLAYBAND to render a richedit control to an
output device (such as a bitmap.canvas(.handle)). (search for those messages
in the Windows SDK / Help included in Delphi)
i did this once and it worked fine for me. but beware, it´s slow. if you got
heavy load on your database server and you´ve got the chance, delay the
generation of the bitmaps or let another pc generate the bitmaps. if the
rendering is done on the client machines you´re fine.
B.
"Bob Berry" <XXXX@XXXXX.COM>schrieb im Newsbeitrag
Quote
I have (among others) two fields in a database.

CUE is a memo field into which I have stored richtext. I want to store
that
text into a bitmap and save the bitmap in a second graphic field called
BMPVIDEO, and I'd like BMPVIDEO to display bold, italic, etc.

The mechanics all work fine, except that BMPVIDEO contains the richtext
formatting characters. I create the bitmap with
Canvas.TextOut(20, 20, FieldByName('CUE').AsString);

I write BMPVIDEO with a SaveToStream.

How can I get formatted text from richtext into a bitmap?


 

Re:Richtext in a bitmap

In article <3fa9d856$XXXX@XXXXX.COM>, Bob Berry writes:
Quote
CUE is a memo field into which I have stored richtext. I want to store that
text into a bitmap and save the bitmap in a second graphic field called
BMPVIDEO, and I'd like BMPVIDEO to display bold, italic, etc.

The mechanics all work fine, except that BMPVIDEO contains the richtext
formatting characters. I create the bitmap with
Canvas.TextOut(20, 20, FieldByName('CUE').AsString);

I write BMPVIDEO with a SaveToStream.

How can I get formatted text from richtext into a bitmap?
You tell the richedit to render itself to the bitmaps canvas, using the
EM_FORMATRANGE message.
Painting a rich edit control onto a bitmap for preview:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, ComCtrls;
type
TForm1 = class(TForm)
RichEdit1: TRichEdit;
Image1: TImage;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses printers, richedit;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
zoomfactor, xScale, yScale: Single;
pagerect, outputrect : TRect;
fmtRange: TFormatRange;
begin
zoomfactor := 0.5;
xScale := Screen.PixelsPerInch / GetDeviceCaps( Printer.handle, LOGPIXELSX
);
yScale := Screen.PixelsPerInch / GetDeviceCaps( Printer.handle, LOGPIXELSY
);
// Size bitmap to 50% of size of a printer page and fill it white
With image1.Picture.Bitmap Do Begin
Width := Round( Printer.Pagewidth * zoomfactor * xScale );
Height:= Round( Printer.PageHeight * zoomfactor * yScale );
With Canvas Do Begin
Brush.Color := clWhite;
Brush.Style := bsSolid;
FillRect( Cliprect );
End;
End;
// scale the bitmap canvas according to the zoomfactor
With image1.Picture.Bitmap.Canvas Do Begin
SetMapMode( handle, MM_ANISOTROPIC );
SetWindowExtEx(handle,
Screen.PixelsPerInch, Screen.PixelsPerInch,
Nil);
SetViewportExtEx(handle,
Round(Screen.PixelsPerInch * zoomfactor),
Round(Screen.PixelsPerInch * zoomfactor),
Nil);
End;
// set up a page rectangle for the rich edit control and
// an output area inside, which gives us some margins. The
// units here are twips (1/1440 inch).
With image1.Picture.Bitmap Do
pagerect := Rect( 0, 0,
Round(width * 1440 / Screen.PixelsPerInch / xScale),
Round(height * 1440 / Screen.PixelsPerInch / yScale) );
outputrect := pagerect;
InflateRect( outputrect, -720, -720 ); // 1/2 inch margin
// set up the parameter record for EM_FORMATRANGE
fillChar( fmtRange, sizeof(fmtrange), 0);
With fmtRange Do Begin
hDC := image1.Picture.Bitmap.Canvas.Handle;
hdcTarget := hDC;
rc := outputrect;
rcPage := pagerect;
chrg.cpMin := 0;
chrg.cpMax := richedit1.GetTextLen-1;
End;
// format the text
richedit1.Perform( EM_FORMATRANGE, 1, Longint(@fmtRange));
// Free cached information
richedit1.Perform( EM_FORMATRANGE, 0, 0);
end;
end.
Peter Below (TeamB)
Use the newsgroup archives :
www.mers.com/searchsite.html
www.tamaracka.com/search.htm
groups.google.com
www.prolix.be
 

Re:Richtext in a bitmap

My thanks to b. zimmermann and Peter Below for their replies. I could not
find any Help file information on the suggested approach so I tried to
follow Mr. Below's example. I'd appreciate it if someone could tell me
where Help info is available on this subject.
I am apparently writing something to the database because when I try to look
at the BMPVIDEO fields in Paradox I get, "Blob has invalid header." Any
further advice and guidance would be greatly appreciated. The code for my
attempt to rather blindly follow the example is below:
{ dbredtText is a DB rich edit control that is tied to a Paradox memo field
imgBitmap is an image control
BMPVIDEO is a Paradox graphic field
}
var
bstream: TStream;
fmtrange: TFormatRange;
displayrect, outputrect: TRect;
begin
{ Set up a display rectangle for the rich edit control and an output area
inside. The units for InflateRect are twips (1/1440 inch).}
with imgBitmap.Picture.Bitmap do
begin
Width := 1024;
Height := 150;
with Canvas do
begin
SetMapMode(handle, MM_ANISOTROPIC);
SetWindowExtEx(handle, 1024, 150, nil);
SetViewportExtEx(handle, 1024, 150, nil);
end;
displayrect := Rect(0, 0, 1024, 150);
end;
outputrect := displayrect;
InflateRect(outputrect, -180, -180); // 1/8 inch margins
// Create all BMPVIDEO fields
with tblLibretto do
begin
First;
while not Eof do
begin
Edit;
// Set up the parameter record for EM_FORMATRANGE
FillChar(fmtrange, sizeof(fmtrange), 0);
with fmtrange do
begin
hDC := imgBitmap.Picture.Bitmap.Canvas.Handle;
hdcTarget := hDC;
rc := outputrect;
rcPage := displayrect;
chrg.cpMin := 0;
chrg.cpMax := dbredtText.GetTextLen - 1;
end;
// Format the text
dbredtText.Perform(EM_FORMATRANGE, 1, Longint(@fmtrange));
bstream := CreateBlobStream(FieldByName('BMPVIDEO'), bmWrite);
try
bstream.Position := 0;
imgBitmap.Picture.Bitmap.SaveToStream(bstream);
finally
bstream.Free;
dbredtText.Perform(EM_FORMATRANGE, 0, 0); // Free cached information
end; //try
Post;
MoveBy(1);
end; //while not Eof
end; //with tblLibretto
"Bob Berry" <XXXX@XXXXX.COM>writes
Quote
I have (among others) two fields in a database.

CUE is a memo field into which I have stored richtext. I want to store
that
text into a bitmap and save the bitmap in a second graphic field called
BMPVIDEO, and I'd like BMPVIDEO to display bold, italic, etc.

The mechanics all work fine, except that BMPVIDEO contains the richtext
formatting characters. I create the bitmap with
Canvas.TextOut(20, 20, FieldByName('CUE').AsString);

I write BMPVIDEO with a SaveToStream.

How can I get formatted text from richtext into a bitmap?


 

Re:Richtext in a bitmap

In article <3faab74d$XXXX@XXXXX.COM>, Bob Berry writes:
Quote
My thanks to b. zimmermann and Peter Below for their replies. I could not
find any Help file information on the suggested approach so I tried to
follow Mr. Below's example. I'd appreciate it if someone could tell me
where Help info is available on this subject.
The message in question is documented in the Microsoft Platform SDK help files
(if you have the SDK installed), or on msdn.microsoft.com. The win32.hlp file
that comes with Delphi (optional install item!) contains an old version of the
SDK docs (several years out of date now, since MS abandoned the Winhelp file
format for the docs).
Quote
I am apparently writing something to the database because when I try to look
at the BMPVIDEO fields in Paradox I get, "Blob has invalid header." Any
further advice and guidance would be greatly appreciated. The code for my
attempt to rather blindly follow the example is below:
I think the problem is the way you store the bitmap into the field. Paradox
sems to require a special header in front of the bitmap data. The TDBImage
component adds that header when storing itself into a BLOB field, if memory
serves. You may want to look at its source code.
Peter Below (TeamB)
Use the newsgroup archives :
www.mers.com/searchsite.html
www.tamaracka.com/search.htm
groups.google.com
www.prolix.be