C# 如何将 IPictureDisp 转换为 System.Drawing.Image
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/468972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert IPictureDisp to System.Drawing.Image
提问by Dirk Vollmar
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.
从 COM 库(Microsoft Office Document Imaging aka MODI)我收到一个图像作为 IPictureDisp,我想将其转换为 System.Drawing.Image 对象。
What would be the best way to do that?
什么是最好的方法呢?
Currently I'm using the code below, which however throws an NotImplementedException.
目前我正在使用下面的代码,但是它会抛出一个 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?
我在我的项目中引用了 stdole、System.Windows.Forms 和 System.Drawing。我错过了什么吗?
采纳答案by Dirk Vollmar
As it seems the picture obtained from the Microsoft Office Document Imaging COM components is not a valid IPictureDisp object and there seems no way to convert it.
从 Microsoft Office Document Imaging COM 组件获得的图片似乎不是有效的 IPictureDisp 对象,似乎无法对其进行转换。
回答by John K?llén
Have you tried:
你有没有尝试过:
picture1.image = Microsoft.VisualBasic.Compatibility.VB6.Support.IPictureDispToImage(pict)
回答by Dirk Vollmar
Microsoft.VisualBasic.Compatibility.VB6.Support.IPictureDispToImage is not guaranteed to always be included in future updates. So... taking the example from http://support.microsoft.com/kb/555417I came up with the following
Microsoft.VisualBasic.Compatibility.VB6.Support.IPictureDispToImage 不保证总是包含在未来的更新中。所以......以http://support.microsoft.com/kb/555417为例,我想出了以下内容
Example
例子
Public Class ImageToPictureDispConverter
公共类 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
End Class
结束类
I see in your constructor that you pass an empty string. I ended up having to pass the following string "{63109182-966B-4e3c-A8B2-8BC4A88D221C}". If I passed an empty string I received a system.formatexception error. It looks like you have everything I have except for this string in your call to base.
我在你的构造函数中看到你传递了一个空字符串。我最终不得不传递以下字符串“{63109182-966B-4e3c-A8B2-8BC4A88D221C}”。如果我传递了一个空字符串,我会收到一个 system.formatexception 错误。看起来你拥有我所有的一切,除了你对 base 的调用中的这个字符串。
Hope this helps.
希望这可以帮助。
回答by petr k.
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:
Olivier Jacot-Descombes:上面的链接已损坏。我已经从 Internet Archive WayBackMachine添加了相应的链接:
Converting between IPictureDisp and System.Drawing.Image(MSDN Blogs > Andrew Whitechapel).
在 IPictureDisp 和 System.Drawing.Image 之间转换(MSDN 博客 > Andrew Whitechapel)。
回答by Ama
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
Inspired from this post in the MSDN Forum.
No idea why the CType(MyIPicture.Type, Integer) = 1
is required, but it works..
灵感来自MSDN 论坛中的这篇文章。不知道为什么CType(MyIPicture.Type, Integer) = 1
需要,但它的工作原理..