2009-01-22 75 views
2

從COM庫(Microsoft Office Document Imaging aka MODI)我收到一個圖像作爲IPictureDisp,我想將其轉換爲System.Drawing.Image對象。如何將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中。我錯過了什麼嗎?

回答

2

,因爲它似乎從Microsoft Office Document Imaging中的COM組件獲得的圖片不是有效的IPictureDisp對象,似乎沒有辦法把它轉換。

0

您是否嘗試過:

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

IPictureDispToImage()應該位於Support命名空間中嗎?但是,我得到相同的NotImplementedException。 – 2009-01-22 13:03:49

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錯誤。它看起來像你有我擁有的一切,除了在你的基地調用這個字符串。

希望這會有所幫助。

2

看看這個article

它描述了三個不同的選項,只是選擇一個你覺得最簡單或「最乾淨」爲你的目的(包括你聲稱不工作的人)。

+0

謝謝,這是一篇很棒的文章。我上面的示例代碼實際上來自該網站。 – 2009-02-10 00:17:38

相關問題