Thema: Delphi Bitmap aus RTF erzeugen

Einzelnen Beitrag anzeigen

HolgerX

Registriert seit: 10. Apr 2006
Ort: Leverkusen
969 Beiträge
 
Delphi 6 Professional
 
#10

AW: Bitmap aus RTF erzeugen

  Alt 23. Jun 2017, 13:37
Hmm..

Hab mal nen bischen gegooglet und zusmmengeschripselt:
(Vergessen die Quellen zu notieren.. )


Delphi-Quellcode:
uses
  Windows, Classes, Graphics, ComCtrls, RichEdit;

procedure DrawRTF(DestDCHandle: HDC; const R: TRect;
  RichEdit: TRichEdit; PixelsPerInch: Integer);
var
  TwipsPerPixel: Integer;
  Range: TFormatRange;
begin
  TwipsPerPixel := 1440 div PixelsPerInch;

  Range.hDC := DestDCHandle; {DC handle}
  Range.hdcTarget := DestDCHandle; {ditto}
  {Convert the coordinates to twips (1/1440")}
  Range.rc.Left := R.Left * TwipsPerPixel;
  Range.rc.Top := R.Top * TwipsPerPixel;
  Range.rc.Right := R.Right * TwipsPerPixel;
  Range.rc.Bottom := R.Bottom * TwipsPerPixel;
  Range.rcPage := Range.rc;
  {Start at character zero}
  Range.chrg.cpMin := 0;
  {Display all Characters}
  Range.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;


procedure RTFToBmp(MainWND : HWND;AFileName : string;var ABitMap : TBitmap);
var
  RichEdit: TRichEdit;
  DestDCHandle: HDC;
begin
  RichEdit :=TRichEdit.CreateParented(MainWND);
  try
    RichEdit.Width := GetDeviceCaps(GetDC(RichEdit.Handle),VERTRES);
    RichEdit.Height := GetDeviceCaps(GetDC(RichEdit.Handle),HORZRES);

    RichEdit.Visible := False;
    RichEdit.Lines.LoadFromFile(AFileName);

    ABitMap.width := RichEdit.Width;
    ABitMap.height := RichEdit.Height;
    DestDCHandle := ABitMap.Canvas.Handle;

    DrawRTF(DestDCHandle, Rect(0, 0, ABitMap.Width, ABitMap.Height), RichEdit, 96);
  finally
    RichEdit.Free;
  end;
end;

Aufruf:
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var
  tmpBMP : TBitMap;
begin
  if OpenDialog1.Execute then begin
    tmpBMP := TBitMap.Create;
    try
      RTFToBmp(self.Handle,OpenDialog1.FileName,tmpBMP);
      Image1.Picture.Assign(tmpBMP);
    finally
      tmpBMP.Free;
    end;
  end;
end;
Damit erhalte ich ein Bitmap der ersten Seite des RTF Dokumentes...

(Getestet mit D6...)
  Mit Zitat antworten Zitat