How to convert System__ComObject to an image?

I use DSOFile to get summary properties from some Crystal Reports report files. SummaryProperties.Thumbnail returns an object, and I need to convert the object to an image so that I can display it as a preview in my form. I tried to make it System.Drawing.Image , but I get the error "ImageConverter cannot convert from System .__ ComObject."

+3
source share
2 answers

Perhaps it would be a COM interface for images, IPicture or IPictureDisp. To do the conversion, you can use the static method AxHost.GetPictureFromIPicture or GetPictureFromIPictureDisp.

+3
source

In the end, I wrote a small wrapper class that worked for me:

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

You want to use AxHost, as Hans mentioned. It was a little harder than I thought. Please note that you want to use AxHost.GetPictureFromIPicture, not GetPictureFromIPictureDisp.

About AxHost.GetPictureFromIPictureDisp:

. GetPictureFromIPicture, IPictureDisp , , IPictureDisp OLE - IPicture.

:

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

Source: https://habr.com/ru/post/1756781/


All Articles