将图像从位图转换为RTF后,图像不以ms字显示(但在wordpad中显示)

时间:2010-06-22 12:06:31

标签: delphi ms-word bitmap rtf delphi-2007

我正在尝试使用Delphi 2007将位图文件转换为rtf。

我使用下面的代码进行转换。

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;

现在我的问题是我无法在MS Word或Viewer中查看图像。

但是我可以在字垫中查看图像。

请建议我解决这个问题。

3 个答案:

答案 0 :(得分:4)

我认为问题在于RTF渲染的Word实现要求提供比Wordpad更多的信息(我认为出于安全原因 - 避免溢出攻击 - ),但这是我必须承认的纯粹推测。

在描述位图信息时尝试准确:例如,如果位图是32位使用\ wbmbitspixel32,请在rtf编码中使用\ picw和\ pich等放置宽度和高度。可能你运气好了

以下是此示例:

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

答案 1 :(得分:0)

如何将图像转换为文本文件? 我想RTF是RichtTextFormat吗?

我会将bmp并将其与Microsoft Word API一起放入文档中并保存文档为rtf。

托比

答案 2 :(得分:0)

如果要在MS Word或Word Viewer中查看图像,请将图像转换为EMF文件并将其嵌入RTF标记内。 (注意:在这里你无法在Wordpad中查看图像)

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

如果要在Wordpad中查看图像,请将图像转换为位图并将其嵌入RTF标记内。

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

我不知道为什么会这样。