delphi - 从 TOleContainer 中提取位图?

标签 delphi bitmap containers extract ole

我正在尝试使用 IDataObject 的 GetData 方法从 TOleContainer 中提取位图。

 OleContainer1.CreateObject('Paint.Picture', false);
 OleContainer1.OleObjectInterface.QueryInterface(IDataObject, DataObject);

EnumFormatEtc 与 DataObject 上的 DATADIR_GET 返回以下内容:

 cfFormat, ptd, dwAspect, lIndex, tymed

 CF_METAFILEPICT, nil, DVASPECT_CONTENT, -1, TYMED_MFPICT
 CF_DIB, nil, DVASPECT_CONTENT, -1, TYMED_HGLOBAL or TYMED_ISTREAM
 CF_BITMAP, nil, DVASPECT_CONTENT, -1, TYMED_HGLOBAL

但是当我这样做时:

FormatEtc.cfFormat := CF_BITMAP;
FormatEtc.ptd := nil;
FormatEtc.dwAspect := DVASPECT_CONTENT;
FormatEtc.lIndex := -1;
FormatEtc.tymed := TYMED_HGLOBAL;

OleCheck(DataObject.GetData(FormatEtc, StorageMedium));

我收到无效的 FORMATETC 结构错误。我做错了什么?

最佳答案

我通过使用找到的代码 here 做同样的事情。 。就我而言,我发现最好执行以下操作,即使用提供的链接中的 DrawOleOnBmp():

oleMain.UpdateObject;
if oleMain.OleObjectInterface = nil then
  raise Exception.Create('OLE Container is empty.');
DrawOleOnBmp(oleMain.OleObjectInterface, imgMain.Bitmap);
imgMain.Bitmap.SaveToFile('Filename.bmp');

其中 oleMainTOleContainerimgMainTImage32。两者都在表单上可见...

为方便起见,以下是链接中的方法,由 @MarkElder 编写:

{
  DrawOleOnBmp
  ---------------------------------------------------------------------------
  Take a OleObject and draw it to a bitmap canvas.  The bitmap will be sized
  to match the normal size of the OLE Object.
}
procedure DrawOleOnBmp(Ole: IOleObject; Bmp: TBitmap32);
var
  ViewObject2: IViewObject2;
  ViewSize: TPoint;
  AdjustedSize: TPoint;
  DC: HDC;
  R: TRect;
begin
  if Succeeded(Ole.QueryInterface(IViewObject2, ViewObject2)) then
  begin
    ViewObject2.GetExtent(DVASPECT_CONTENT, -1, nil, ViewSize);

    DC := GetDC(0);
    AdjustedSize.X := MulDiv(ViewSize.X, GetDeviceCaps(DC, LOGPIXELSX), 2540);
    AdjustedSize.Y := MulDiv(ViewSize.Y, GetDeviceCaps(DC, LOGPIXELSY), 2540);
    ReleaseDC(0, DC);

    Bmp.Height := AdjustedSize.Y;
    Bmp.Width := AdjustedSize.X;

    Bmp.FillRect(0, 0, Bmp.Width, Bmp.Height, clWhite);

    SetRect(R, 0, 0, Bmp.Width, Bmp.Height);

    OleDraw(Ole, DVASPECT_CONTENT, Bmp.Canvas.Handle, R);
  end
  else
  begin
    raise Exception.Create('Could not get the IViewObject2 interfact on the OleObject');
  end;
end;

关于delphi - 从 TOleContainer 中提取位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15394110/

相关文章:

delphi - TList 中的内存泄漏

database - 一个细节可以有两个主人吗?

delphi - 如何在一组中设置两个单选按钮,在另一组中设置两个单选按钮?

java - 在 Android 中将位图转换为 TIFF

c - 位图文件没有以正确的顺序保存在内存中

c++ - 用于中间插入和随机访问的性能良好的顺序容器

正则表达式 - 查找不包含子字符串的多行字符串

java - Android位图和捕获屏幕

wordpress - Nginx 配置到容器内的 Wordpress

containers - 尝试启用 approle 时出错