RTF 图像支持非常有限(Windows 支持,不仅在 Delphi 中),位图和元文件以外的其他格式也可以工作,但 RichEdit 控件不能正确显示。
我注意到的一件事是,当 Microsoft Word 图像被复制并粘贴到 RTF 中时,它可以平滑地缩放,而不是手动粘贴图像(作为位图)。原因是 Word 在内部保留 Metafile 格式的图像的缩放预览(以及原始图像),并且此缩放版本被复制并粘贴到 RTF 中,显然 RTF 在 RichText 编辑器中缩放时可以平滑地呈现 MetaFile 图像。
似乎是一个很好的解决方法,在 WPF 函数中实现嵌入 BMP 后,我注意到一个我无法解决的问题:生成的 WMF 是位图大小的两倍。看起来 WMF 存储了绘制缓冲区或图像的第二个副本。
编码:
procedure DoCopyImage(AGraphic: TGraphic; AWidth, AHeight: Integer);
var
mf: TMetafile;
mfc: TMetafileCanvas;
r: Cardinal;
begin
mf := TMetafile.Create;
try
mf.Enhanced := True;
mf.SetSize(AWidth, AHeight);
mfc := TMetafileCanvas.Create(mf, 0);
try
// set clipping region to a whole image
r := CreateRectRgn(0, 0, AWidth, AHeight);
try
SelectClipRgn(mfc.Handle, r)
finally
DeleteObject(r);
end;
if (AGraphic.Width = AWidth) and (AGraphic.Height = AHeight) then
mfc.Draw(0, 0, AGraphic)
else
mfc.StretchDraw(Rect(0, 0, AWidth, AHeight), AGraphic);
finally
mfc.Free;
end;
// Clipboard.Assign(mf);
mf.SaveToFile('C:\4MB_MetaFile_Why.wmf');
finally
mf.Free;
end;
end;
我使用 TBitmap 作为 TGraphic 来调用它:
pic := TPicture.Create;
pic.LoadFromFile('C:\2MB_24bpp_Bitmap.bmp');
bmp := Graphics.TBitmap.Create;
bmp.Assign(pic.Graphic);
bmp.Dormant; // experimentation
bmp.FreeImage; // experimentation
DoCopyImage(bmp, bmp.Width, bmp.Height);
有人可以找到这种行为的解释吗?WMF 是否将绘画缓冲区与位图一起存储?如何预防?