如何将IPictureDisp转换为System.Drawing.Image

时间:2009-01-22 12:44:14

标签: c# interop

从COM库(Microsoft Office Document Imaging aka MODI)我收到一个IPictureDisp图像,我想将其转换为System.Drawing.Image对象。

最好的方法是什么?

目前我正在使用下面的代码,但是会抛出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);

这是异常堆栈跟踪:

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)

我在项目中引用了stdole,System.Windows.Forms和System.Drawing。我错过了什么吗?

5 个答案:

答案 0 :(得分:3)

查看此article

它描述了三种不同的选择,只需选择一个您认为最简单或最干净的选项(包括您声称不为您工作的那个)。

答案 1 :(得分:2)

看起来从Microsoft Office Document Imaging COM组件获取的图片不是有效的IPictureDisp对象,似乎无法转换它。

答案 2 :(得分:0)

你试过了吗?

picture1.image = Microsoft.VisualBasic.Compatibility.VB6.Support.IPictureDispToImage(pict)

答案 3 :(得分:0)

Microsoft.VisualBasic.Compatibility.VB6.Support.IPictureDispToImage不保证始终包含在将来的更新中。所以......以http://support.microsoft.com/kb/555417为例,我想出了以下内容

实施例

公共类ImageToPictureDispConverter

Inherits System.Windows.Forms.AxHost

Public Sub New()
    MyBase.New("{63109182-966B-4e3c-A8B2-8BC4A88D221C}")
End Sub

Public Function GetImageFromIPictureDisp(ByVal objImage As stdole.IPictureDisp) As System.Drawing.Image
    Dim objPicture As System.Drawing.Image
    objPicture = CType(MyBase.GetPictureFromIPicture(objImage), System.Drawing.Image)

    Return objPicture
End Function

结束课程

我在你的构造函数中看到你传递一个空字符串。我最后不得不传递以下字符串“{63109182-966B-4e3c-A8B2-8BC4A88D221C}”。如果我传递一个空字符串,我收到一个system.formatexception错误。看起来你拥有我所拥有的一切,除了你对基地的调用中的这个字符串。

希望这有帮助。

答案 4 :(得分:0)

Function GetImage(MyIPicture As stdole.IPictureDisp) As Drawing.Image
    If CType(MyIPicture.Type, Integer) = 1 then
        Return = Drawing.Image.FromHbitmap(MyIPicture.Handle, MyIPicture.hPal)
    else
        Throw New ArgumentException("Image not supported")
    End if
End Function

灵感来自this post in the MSDN Forum。 不知道为什么需要CType(MyIPicture.Type, Integer) = 1,但是有效。.