从TOleContainer中提取位图?

时间:2013-03-13 18:59:30

标签: delphi bitmap containers extract ole

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

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

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

 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结构错误。我做错了什么?

1 个答案:

答案 0 :(得分:1)

我使用找到的代码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');

其中oleMainTOleContainer,而imgMainTImage32。两者都在表格中可见......

为方便起见,这里是链接的方法,由@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;