c# - 如何将 IPictureDisp 转换为 System.Drawing.Image

标签 c# interop

从 COM 库(Microsoft Office Document Imaging 又名 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。我错过了什么吗?

最佳答案

看看这个 article .

它描述了三种不同的选项,只需选择您认为最简单或“最干净”的选项(包括您声称不适合您的选项)。

<小时/>

Olivier Jacot-Descombe:上面的链接已损坏。我已添加来自互联网文件馆的相应链接 WayBackMachine :

Converting between IPictureDisp and System.Drawing.Image (MSDN 博客 > Andrew Whitechapel)。

关于c# - 如何将 IPictureDisp 转换为 System.Drawing.Image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/468972/

相关文章:

c# - 用于网址检查的正则表达式

c# - 需要 Func 提供给 IEnumerable 和 IQueryable 的 Where() 方法

c# - PowerBuilder 中的 .NET 互操作

C# Interop with C vs Interop with Java : Which is better/easier/faster?

c# - 切片器连接不显示可透视的更改后数据源

c# - 给定当前记录的 ID,这是选择上一条和下一条记录的正确方法吗?

c# - 紧凑的框架 - 离线时 HttpWebRequest 资源泄漏

c# - 将多个 sql 行与不同的列组合起来

excel - 如何确定 Excel UDF 来自哪个 DLL?

c# - 初始化 C# IntPtr 以接受来自非托管 C++ DLL 的数据?