How to extract metafile from TOleContainer?

≡放荡痞女 提交于 2019-11-30 22:33:22

OLE Container has on underlying IOLEObject interface you can access. You can pass that to the OLEDraw function with your own canvas. You could use either a Bitmap or Metafile canvas and then save out the image in the format you need.

OleDraw(OleContainer.OleObjectInterface, DVASPECT_CONTENT, Bmp.Canvas.Handle, R);


{
  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: TBitmap);
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;

    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;

When you use the SaveAsDocument method of your OleContainer, a compound document is created. That document will contain an IStream with a name #2OlePress000 (#2 is byte value 2). The contents of this stream is a cached representation of the equation and is used to show it on computers that don't have the equation editor installed.

If you know the format of that stream, maybe you can use it to create an image to show on a webpage.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!