开发者

Image not displaying in ms word after converting it from Bitmap to RTF (but displaying in wordpad)

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.


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜