views:

132

answers:

4

Hi,

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.

A: 

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
+3  A: 

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
A: 

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
A: 

Try this;

uses
  math;

function BitmapToRTF(pict: TBitmap): string;
var
  [...]
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 ';
  rtf := Format('{\rtf1 {\pict\dibitmap0\wbmwidthbytes%d\picw%d\pich%d ',
      [Ceil(pict.Width / 16) * 2, pict.Width, pict.Height]);
  [...]


..and see the Pictures section in the RTF spec for reasons. AAMOF if you had take someone's answer seriously you'd have achieved it already.. ;-)

Sertac Akyuz
Thank you for your reply. I have used all those options but still not able to find solution. But i was able to solve this problem by converting the bitmap to emf file (As client will view the file in ms word but not in wordpad)
Bharat