Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert IPictureDisp to System.Drawing.Image

Tags:

c#

interop

From a COM library (Microsoft Office Document Imaging aka MODI) I receive an image as an IPictureDisp which I would like to convert to a System.Drawing.Image object.

What would be the best way to do that?

Currently I'm using the code below, which however throws an NotImplementedException.

internal sealed class IPictureDispHost : AxHost
{
    /// <summary>
    /// Default Constructor, required by the framework.
    /// </summary>
    private IPictureDispHost() : base(string.Empty) { }
    /// <summary>
    /// Convert the image to an ipicturedisp.
    /// </summary>
    /// <param name="image">The image instance</param>
    /// <returns>The picture dispatch object.</returns>
    public new static object GetIPictureDispFromPicture(Image image)
    {
        return AxHost.GetIPictureDispFromPicture(image);
    }
    /// <summary>
    /// Convert the dispatch interface into an image object.
    /// </summary>
    /// <param name="picture">The picture interface</param>
    /// <returns>An image instance.</returns>
    public new static Image GetPictureFromIPicture(object picture)
    {
        return AxHost.GetPictureFromIPicture(picture);
    }
}

...

// somewhere later the conversion gets called
Image image = IPictureDispHost.GetPictureFromIPicture(picture);

This is the exception stack trace:

System.NotImplementedException: The method or operation is not implemented.
   at System.Windows.Forms.UnsafeNativeMethods.IPicture.GetHandle()
   at System.Windows.Forms.AxHost.GetPictureFromIPicture(Object picture)
   at DocumentViewer.IPictureDispHost.GetPictureFromIPicture(Object picture)

I have references to stdole, System.Windows.Forms and System.Drawing in my project. Am I missing something?

like image 574
Dirk Vollmar Avatar asked Mar 01 '23 01:03

Dirk Vollmar


1 Answers

Check out this article.

It describes three different options to take, just pick the one you find easiest or "cleanest" for your purposes (including the one you claim not to be working for you).


Olivier Jacot-Descombes: The link above is broken. I've added the corresponding link from the Internet Archive WayBackMachine:

Converting between IPictureDisp and System.Drawing.Image (MSDN Blogs > Andrew Whitechapel).

like image 172
petr k. Avatar answered Mar 07 '23 22:03

petr k.