[英]Extract bitmap from TOleContainer?
I'm trying to extract bitmap from TOleContainer
using the IDataObject's GetData method. 我正在尝试使用IDataObject的GetData方法从
TOleContainer
提取位图。
OleContainer1.CreateObject('Paint.Picture', false);
OleContainer1.OleObjectInterface.QueryInterface(IDataObject, DataObject);
EnumFormatEtc with DATADIR_GET
on DataObject returns the following: 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
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. 我收到了无效的FORMATETC结构错误。 What am I doing wrong?
我究竟做错了什么?
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: 在我的情况下,我发现最好执行以下操作,它在提供的链接中使用
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');
Where oleMain
is a TOleContainer
, and imgMain
is a TImage32
. 其中
oleMain
是TOleContainer
,而imgMain
是TImage32
。 Both are visible on the form... 两者都在表格上可见......
For convenience, here is the method from the link, written by @MarkElder: 为方便起见,这里是链接的方法,由@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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.