开发者

How do I convert System__ComObject to an image?

开发者 https://www.devze.com 2023-01-09 06:36 出处:网络
I\'m using DSOFile to get the summary properties from some Crystal Reports report files. SummaryProperties.Thumbnai开发者_高级运维l returns an object and I need to convert the object to an image so I

I'm using DSOFile to get the summary properties from some Crystal Reports report files. SummaryProperties.Thumbnai开发者_高级运维l returns an object and I need to convert the object to an image so I can display it as a preview on my form. I have tried casting it to a System.Drawing.Image but I get an error "ImageConverter cannot convert from System.__ComObject."


It would be a COM interface for images, IPicture or IPictureDisp, probably. You could use the AxHost.GetPictureFromIPicture or GetPictureFromIPictureDisp static method to make the conversion.


I ended up writing a small wrapper class that what worked for me:

stdole.IPictureDisp iPictureDisp = row.Parent.Thumbnail;
pictureBox1.Image = IconTools.GetImage(iPictureDisp);

You'll want to use AxHost, as mentioned by Hans. It was a bit trickier to implement than I first thought though. Note that you'll want to use AxHost.GetPictureFromIPicture rather than GetPictureFromIPictureDisp.

About AxHost.GetPictureFromIPictureDisp:

This method does not work correctly. You can use the GetPictureFromIPicture method to convert an IPictureDisp object into an Image, however, because the IPictureDisp OLE interface is a subset of the IPicture interface.

Here's the wrapper:

public class IconTools
{
    private class IconToolsAxHost : System.Windows.Forms.AxHost
    {
        private IconToolsAxHost() : base(string.Empty) { }

        public static stdole.IPictureDisp GetIPictureDispFromImage(System.Drawing.Image image)
        {
            return (stdole.IPictureDisp)GetIPictureDispFromPicture(image);
        }

        public static System.Drawing.Image GetImageFromIPicture(object iPicture)
        {
            return GetPictureFromIPicture(iPicture);
        }
    }

    public static stdole.IPictureDisp GetIPictureDisp(System.Drawing.Image image)
    {
        return IconToolsAxHost.GetIPictureDispFromImage(image);
    }

    public static System.Drawing.Image GetImage(stdole.IPicture iPicture)
    {
        return IconToolsAxHost.GetImageFromIPicture(iPicture);
    }

    public static System.Drawing.Image GetImage(stdole.IPictureDisp iPictureDisp)
    {
        return IconToolsAxHost.GetImageFromIPicture(iPictureDisp);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消