By Andrew


2010-05-18 20:42:17 8 Comments

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

How can I extract the vector metafile from the formula image to insert it into web-page or some other document without OLE support?

TOleContainer has only 'Equation.3' objects inside, no other possibilities. I've tried to use .Copy method to make it through clipboard, but it's copied an empty image.

2 comments

@Mark Elder 2010-11-17 16:07:35

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;

@Andrew 2010-11-18 06:11:33

Yes, this solution surely much more simple than SaveAsDocument. Thank you, Mark.

@James L. 2011-11-05 01:01:15

You can also use GR32 if you first fill the canvas with a solid color (e.g., White). Simply change the TBitmap to a TBitmap32 and add GR32 to your uses clause. Even if you don't use TBitmap32, it is a good idea to fill the canvas with a solid color, or the OleDraw will simply add the OLEObject's image data to whatever is already on the canvas, like this: Bmp.FillRect(0, 0, Bmp.Width, Bmp.Height, clWhite);

@The_Fox 2010-05-19 08:01:42

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.

Related Questions

Sponsored Content

19 Answered Questions

[SOLVED] How is Docker different from a virtual machine?

1 Answered Questions

[SOLVED] Paste OLE(Object Linking & Embedding) object directly into web page

0 Answered Questions

Grabbing text in a hidden window

  • 2014-12-11 18:56:43
  • user2192404
  • 72 View
  • 0 Score
  • 0 Answer
  • Tags:   delphi

1 Answered Questions

[SOLVED] Delphi - Using OLE causes error in Word

  • 2014-08-26 12:30:56
  • user1009073
  • 1214 View
  • 2 Score
  • 1 Answer
  • Tags:   delphi ole

1 Answered Questions

.NET api for document screenshot?

  • 2012-02-06 13:59:35
  • Liam
  • 261 View
  • 1 Score
  • 1 Answer
  • Tags:   .net ms-office ole

1 Answered Questions

[SOLVED] Extract bitmap from TOleContainer?

0 Answered Questions

Getting image(metafile) from Excel OLE object C#

1 Answered Questions

2 Answered Questions

Import Images from File delphi

  • 2011-08-17 20:13:54
  • Michael
  • 2687 View
  • 0 Score
  • 2 Answer
  • Tags:   image delphi import

1 Answered Questions

[SOLVED] how to reload saved "Embed Source" clipboard data?

Sponsored Content