Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

OCX Snapshot

0.00/5 (No votes)
9 Feb 2000 1  
How to take a snapshot of an OCX without using HWNDs.
  • Download demo project - 33 Kb
  • Download source files - 27 Kb
  • Sample Image - ViewObjSnap.jpg

    Recently, the company I CO-OP for (Bradley Ward Systems) needed a way to show complex data reports over the web. We already have lots of custom OCXs that present these reports in graph or chart form. These OCXs are used in our custom applications - they were never intended to be embedded in web pages.

    Because we wanted to make the web reports as light-weight as possible, refitting the OCXs to also be embedded in web pages was simply not an option - we didn't want to impose stringent system requirements on the computers that would be used to view these reports. We decided that the best way to show these reports over the web was to take a "snapshot" of one of our OCXs and write it out to a JPEG for display on a web page.

    So, our biggest problem was how to get a picture of one of our OCXs... We tried various ways of getting the picture by using the HWND of a form our OCX was embedded on. There were two major problems with this: first, a visible form would have to appear on the screen of the webserver whenever we wanted to get a picture of an OCX; second, a user would always have to be logged onto the webserver in order to create the form that the OCX was embedded on. We simply didn't find a way around these problems.

    So, instead of using the HWND to trick the OCX into painting into a DC of our creation, we went a little deeper...

    Every OCX must implement the standard IViewObject interface - of which one of the methods is Draw(). Sounds perfect, right? Almost - we also have to determine the dimensions of the OCX, functionality which IViewObject does not expose. We used the IOleObject interface's GetExtents() for this. I wrote a small ActiveX control that makes these calls, and returns an IPicture object for embedding in a Picture control on a VB form.

    This ActiveX control I present here - called ViewObjSnap - is a scaled-down version of the one I wrote at work. It doesn't write the picture out to a JPG or anything like that, so you would need to modify it a little to be useful in a real world situation. It is only intended to demonstrate an interesting solution to our problem.

    The source code for the ViewObjSnap control is contained in the ViewObjSnap_src.zip file. The source code and a demo VB project, along with the compiled EXE for those that don't own VB, are in ViewObjSnap_demo.zip.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here