Reputation: 897
I'm trying to extract bitmap from TOleContainer
using the IDataObject's GetData method.
OleContainer1.CreateObject('Paint.Picture', false);
OleContainer1.OleObjectInterface.QueryInterface(IDataObject, DataObject);
EnumFormatEtc with DATADIR_GET
on DataObject returns the following:
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
But when I do:
FormatEtc.cfFormat := CF_BITMAP;
FormatEtc.ptd := nil;
FormatEtc.dwAspect := DVASPECT_CONTENT;
FormatEtc.lIndex := -1;
FormatEtc.tymed := TYMED_HGLOBAL;
OleCheck(DataObject.GetData(FormatEtc, StorageMedium));
I'm getting Invalid FORMATETC stucture error. What am I doing wrong?
Upvotes: 3
Views: 1061
Reputation: 9453
I do the same thing you are trying to do by using the code found here. In my case, I found it best to do the following, which uses the DrawOleOnBmp()
in the provided link:
oleMain.UpdateObject;
if oleMain.OleObjectInterface = nil then
raise Exception.Create('OLE Container is empty.');
DrawOleOnBmp(oleMain.OleObjectInterface, imgMain.Bitmap);
imgMain.Bitmap.SaveToFile('Filename.bmp');
Where oleMain
is a TOleContainer
, and imgMain
is a TImage32
. Both are visible on the form...
For convenience, here is the method from the link, written by @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;
Upvotes: 1