4

I am trying to convert a bitmap file to rtf using Delphi 2007.

I used below code for conversion.

function BitmapToRTF(pict: TBitmap): string;
var
  bi, bb, rtf: string;
  bis, bbs: Cardinal;
  achar: ShortString;
  hexpict: string;
  I: Integer;
begin
  GetDIBSizes(pict.Handle, bis, bbs);
  SetLength(bi, bis);
  SetLength(bb, bbs);
  GetDIB(pict.Handle, pict.Palette, PChar(bi)^, PChar(bb)^);
  rtf := '{\rtf1 {\pict\dibitmap0 ';
  SetLength(hexpict, (Length(bb) + Length(bi)) * 2);
  I := 2;
  for bis := 1 to Length(bi) do
  begin
    achar := Format('%x', [Integer(bi[bis])]);
    if Length(achar) = 1 then
      achar := '0' + achar;
    hexpict[I - 1] := achar[1];
    hexpict[I] := achar[2];
    Inc(I, 2);
  end;
  for bbs := 1 to Length(bb) do
  begin
    achar := Format('%x', [Integer(bb[bbs])]);
    if Length(achar) = 1 then
      achar := '0' + achar;
    hexpict[I - 1] := achar[1];
    hexpict[I] := achar[2];
    Inc(I, 2);
  end;
  rtf := rtf + hexpict + ' }}';
  Result := rtf;
end;

Now my problem is i was not able to view the image in MS Word or Viewer.

But i can view the image in word pad.

Please suggest me in solving this problem.

Bharat
  • 6,828
  • 5
  • 35
  • 56
  • You mean that you convert a BMP file to a RTF file with an embedded BMP file? – Andreas Rejbrand Jun 22 '10 at 12:25
  • Yes i mean that, embedding a bmp into rtf document – Bharat Jun 22 '10 at 12:27
  • 1
    Take the bmp file, manually include it in Word, save the document in the rtf format. Look at the differences in the saved file compared to yours. Try stripping the rtf file by deleting seemingly superfluous commands to find out which are necessary for Word. But this is cumbersome as different Word versions have different rtf versions they support. – Ralph M. Rickenbach Jun 28 '10 at 11:33

3 Answers3

4

I think the problem is that the Word implementation for RTF rendering asks for more information than the Wordpad's one (I think that for security reasons -avoid overflow attacks-), but this is pure speculation I must confess.

Try being accurate when describing your bitmap info: for example if the bitmap is 32-bit use \wbmbitspixel32, put the width and height in your rtf encoding with \picw and \pich, etc. May be you have luck with that.

Here is an example of this:

http://www.scribd.com/doc/25967552/Rtf1-Ansi-Ansicpg1252-Uc2-Deff0-Deflang1033-Fonttbl-f0-Froman-Fcharset0-Fprq2-Panose-02020603050405020304-Times-New-Roman-f1-Fswiss-Fchar

someone
  • 916
  • 1
  • 5
  • 8
0

How can you convert an image to a textfile? RTF is RichtTextFormat i guess?

I would take the bmp and put it with the Microsoft Word API into a Document and save the document was rtf.

Tobi

Tobi
  • 540
  • 7
  • 18
0

If you want to view the image in MS Word or Word Viewer, convert the image to EMF file and embed it inside the RTF tags. (Note: here you cant view the image in Wordpad)

{\rtf1 {\pict\emfblif <emf source> }}

If you want to view the image in Wordpad, convert the image into bitmap and embed it inside the RTF tags.

{\rtf1 {\pict\dibitmap0 <bitmap source> }}

I dont know why this happens.

Bharat
  • 6,828
  • 5
  • 35
  • 56