How to extract metafile from TOleContainer?

I have a Delphi application (BDS 2006) with TOleContainer control. It has an OLE object inside the MS Equation formula (name "Equation.3") from MS Office 2003.

How to extract a vector metafile from a formula image to paste it into a web page or some other document without OLE support?

TOleContainer has only "Equation.3" objects inside, there are no other options. I tried using the .Copy method to do this through the clipboard, but it copied a blank image.

+4
source share
2 answers

The OLE container has the basic IOLEObject interface, which you can access. You can pass this OLEDraw using your own canvas. You can use the Bitmap or Metafile canvas, and then save 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; 
+4
source

When you use the SaveAsDocument method of your OleContainer, a compound document is created. This document will contain an IStream named # 2OlePress000 (# 2 is the value of byte 2). The content of this stream is a cached representation of the equation and is used to display it on computers that do not have the equation editor installed.

If you know the format of this stream, perhaps you can use it to create an image for display on a web page.

+3
source

Source: https://habr.com/ru/post/1310167/


All Articles