ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Paint RichFormat Layer on a Bitmap Transparently
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

john_siggy@yahoo.com

USA
158 Posts

Posted - Oct 07 2019 :  13:14:38  Show Profile  Reply
Hi Nigel,

Trying to paint a RichText Layer onto a Background TBitmap with transparency. Below are two procedures that implement this with Delphi's TRichEdit control and a bitmap. Don't want to use layers because this procedure is called many times per second.

I have tried to assign a RichText Layer to Delphi's TRichEdit control but I get text without formatting. The text looks like a Memo control with it's one font. Control characters are revealed
(e.g. {\rtf1\ansi\deff0\deflang1033{\fonttbl{\f0\fswiss Arial;}...).

How to paint RichText Layer onto a bitmap with formatting preserved ??





///////////////////////////////////////////

procedure DrawRTF(DestDCHandle: HDC; const R: TRect;
  RichEdit: TRichEdit; PixelsPerInch: Integer);
var
  TwipsPerPixel: Integer;
  Range: TFormatRange;
begin
  TwipsPerPixel := 1440 div PixelsPerInch;
  with Range do
  begin
    hDC := DestDCHandle; {DC handle}
    hdcTarget := DestDCHandle; {ditto}
    {Convert the coordinates to twips (1/1440")}
    rc.Left := R.Left * TwipsPerPixel;
    rc.Top := R.Top * TwipsPerPixel;
    rc.Right := R.Right * TwipsPerPixel;
    rc.Bottom := R.Bottom * TwipsPerPixel;
    rcPage := rc;
    chrg.cpMin := 0;
    chrg.cpMax := -1; {RichEdit.GetTextLen;}
    {Free cached information}
    RichEdit.Perform(EM_FORMATRANGE, 0, 0);

    {First measure the text, to find out how high the format rectangle will be. The call sets fmtrange.rc.bottom to the actual height required, if all                 characters in the selected range will fit into a smaller rectangle}

    RichEdit.Perform(EM_FORMATRANGE, 0, DWord(@Range));
    {Now render the text}
    RichEdit.Perform(EM_FORMATRANGE, 1, DWord(@Range));
    {Free cached information}
    RichEdit.Perform(EM_FORMATRANGE, 0, 0);
  end;
end;




procedure Tfmain.Button1Click(Sender: TObject);
var
  RichEdit: TRichEdit;
  ExStyle: DWord;
  bmp: TBitmap;
  DestDCHandle: HDC;
begin
  RichEdit := TRichEdit.Create(Self);
  try
    RichEdit.Visible := False;
    RichEdit.Parent := Self;
    {Win2k, WinXP}
    ExStyle := GetWindowLong(RichEdit.Handle, GWL_EXSTYLE);
    ExStyle := ExStyle or WS_EX_TRANSPARENT;
    SetWindowLong(RichEdit.Handle, GWL_EXSTYLE, ExStyle);
    RichEdit.Lines.LoadFromFile('c:\JDS\Readme.rtf');
    bmp := TBitmap.Create;
    try
      bmp.LoadFromFile('c:\jds\TheBackGroundBmp.bmp');
      DestDCHandle := bmp.Canvas.Handle;
      {Win9x}
      SetBkMode(DestDCHandle, TRANSPARENT);
      DrawRTF(DestDCHandle, Rect(0, 0, bmp.Width, bmp.Height), RichEdit, 96);
      Image1.Picture.Assign(bmp);
      Image1.Refresh;
    finally
      bmp.Free;
    end;
  finally
    RichEdit.Free;
  end;
end;

//////////////////////////////////////

john_siggy@yahoo.com

USA
158 Posts

Posted - Oct 07 2019 :  15:26:23  Show Profile  Reply
One solution is to copy from the RichText Layer to Delphi's TRichEdit control and then paint the bitmap. See code at end.

Is there a more direct way to paint the RichText Layer to a Bitmap??


ss := TStringStream.Create(TIETextLayer( ImageEnView1.Layers[1] ).RichText);
ss.Position := 0;
RichEdit.Lines.LoadFromStream(ss);
RichEdit.WordWrap := True;
ss.Free;




Go to Top of Page

xequte

38610 Posts

Posted - Oct 10 2019 :  02:28:05  Show Profile  Reply
Hi

See the TIERichEdit.PaintTo method in iexRichEdit. It will draw transparently onto a bitmap.



Nigel
Xequte Software
www.imageen.com
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: