Link to home
Start Free TrialLog in
Avatar of sz3905
sz3905

asked on

Writing on a bitmap with RichText format

I need to write some richtext formated data(text) to a bitmap. The text is contained in a richtext memo object. I need to get a single line with it's SelAttributes. Onto the bitmap. The attributes would be Ttextattributes only.

EM_GETCHARFORMAT Doesn't seem to work or exists in the messages.unit?

here's a start:

  with richtext1 do
  begin
  // set the line start character index
  selstart := perform( em_lineindex, LineNumer, 0 );
  // set the length of the line  
  SelLength := Length( richtext1.Lines[LineNumer]);
  // get the line into a tmp string
  tmpstr  :=SelText;
  // get the attributes for this line
  tmpatrib:=richtext1.SelAttributes;
  end;

I guess my problem is trying to assign the "tmpatrib" properties to the bitmaps's canvas's font (or something) before writing on the bitmap's canvas.
 
Maybe I'm looking at the problem wrong,don't know.
Thanks for any help!
steve
Avatar of simonet
simonet
Flag of Brazil image


My approach would be:

Copy the contents of the RicheEdit's canvas to a temp bitmap (this will be the "image" of the text, not the text itself). Now set the transparent color of the temp bitmap to the background color of the richedit control (let's say clWhite). All you have to do now is place the transparent temp bitmap on top of the first bitmap.

Yours,

Alex
Athena's Place: http://www.bhnet.com.br/~simonet
Avatar of sz3905
sz3905

ASKER

Thank You Alex, I'll check the concept out.
Would this still work if:
1) The richtext object's canvas is very large (say 5-32k?)
I may need many lines processed, only one line at a time however?
2) The richtext object's canvas is not visible while we're doing this "canvas copy"?
thx!
Avatar of sz3905

ASKER

Alex,
Is the richedit object's canvas exposed?
/steve

Avatar of sz3905

ASKER

Edited text of question.
Avatar of sz3905

ASKER

Edited text of question.
Have a look at TRichEdit.Print in the VCL.
It uses FormatRange (I think) which writes rich text to a DC.
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi, sz3905
Here is another example of drawing rich text on a bitmap's canvas.
By the way, EM_GETCHARFORMAT is defined in RichEdit.pas

procedure ShowBMP(ABMP : TBitmap);
begin
  Form1.Image1.Canvas.Draw(0, 0, ABMP);
  Form1.ShowModal;
end;

procedure DrawRichText;
var Bmp : TBitmap;
begin
  Bmp := TBitmap.Create;
  Bmp.Width := 100;
  Bmp.Height := 100;
  Bmp.Canvas.Brush.Color := clWhite;
  Bmp.Canvas.Brush.Style := bsSolid;
  Bmp.Canvas.FillRect(Rect(0, 0, Bmp.Width, Bmp.Height));
  {Here set the selection in the RichEdit}
  Bmp.Canvas.Font.Name := Editor.SelAttributes.Name;
  Bmp.Canvas.Font.Size := Editor.SelAttributes.Size;
  Bmp.Canvas.Font.Color := Editor.SelAttributes.Color;
  Bmp.Canvas.Font.Style := Editor.SelAttributes.Style;
  Bmp.Canvas.TextOut(10, 10, Editor.SelText);
  ShowBMP(Bmp);
end;

hi kotik,

this idea i had also

this part

  Bmp.Canvas.Font.Name := Editor.SelAttributes.Name;
  Bmp.Canvas.Font.Size := Editor.SelAttributes.Size;
  Bmp.Canvas.Font.Color := Editor.SelAttributes.Color;
  Bmp.Canvas.Font.Style := Editor.SelAttributes.Style;

can replaced with
 
  Bmp.Canvas.Font.Assign(Editor.SelAttributes);

the problem by this line is that the text has a white background

  Bmp.Canvas.TextOut(10, 10, Editor.SelText)

well, it doesnt matter, if the destination-Bitmap is white  
i'm working about to write the text to a bmp with a picture

meikl ;-)
hi sz3905,

thats better, writes the Richtext line on the Bitmap and
leave the Background of the Bitmap as it was,
works now also with colored text.


Procedure CopyLineFromRichEditToBitmap(R : TRichEdit;
                                       Line : Integer;
                                       B : TBitMap;
                                       AtPos : TPoint);
var
  I, C, MH, H, W : Integer;  //Counter, CharPos, MaxHeight, CharHeight, CharWidth
  TB, TB2 : TBitmap;         //Temporary Bitmaps
begin
  TB := TBitmap.Create;      //Create Temporary
  TB2 := TBitmap.Create;
  MH := 0;                   //Initialize
  H := 0;
  TB.Height := 100;
  TB.Width := 5;
  C := R.Perform(EM_LINEINDEX,Line,0);
  for I := 0 to Length(R.Lines[Line]) do  //Paint Line
  begin
    R.Selstart := C+I;
    R.SelLength := 1;
    TB.Canvas.Font.Assign(R.SelAttributes);  //Assign CharAttributes
    W := TB.Canvas.TextWidth(R.SelText);
    TB.Width := TB.Width + W;                //Expand Bitmap
    H := TB.Canvas.TextHeight(R.SelText);
    if MH < H then                           // MaxHeight
      MH := H;
    TB.Canvas.TextOut(TB.Width-W,50-h,R.SelText);  //Paint a Char
  end;
  TB2.Width := TB.Width - 5;  //adjust Painting Rect
  Tb2.Height := MH;
  TB2.Canvas.CopyRect(Rect(0,0,TB2.Width,TB2.Height),TB.Canvas,Rect(5,50-mh,tb.width,50));
  TB2.Transparent := True;   //Prepare
  TB2.TransparentMode := tmFixed;
  TB2.TransparentColor := clWhite;
  B.Canvas.Draw(AtPos.x,AtPos.Y,TB2);  //Paint to Original
  TB2.Free;  //Free Memory
  TB.Free;
end;


meikl
appendix,
by this sample
a line can contain multiple styles, fonts, colors

meikl ;-)
Avatar of sz3905

ASKER

Thanks kotik for your insight.
------------------------------

Thanks Meikl!
I will run your idea. It's being used in a display system so I need to make it faster. Thanks for the Jump Start!
/steve