Ejemplo n.º 1
0
        public static Image?GetPictureFromIPictureDisp(object picture)
        {
            if (picture is null)
            {
                return(null);
            }

            int          hPal = default;
            IPictureDisp pict = (IPictureDisp)picture;
            PICTYPE      type = (PICTYPE)pict.Type;

            if (type == PICTYPE.BITMAP)
            {
                try
                {
                    hPal = pict.hPal;
                }
                catch (COMException)
                {
                }
            }

            Image?image = GetPictureFromParams(pict.Handle, type, hPal, pict.Width, pict.Height);

            GC.KeepAlive(pict);
            return(image);
        }
Ejemplo n.º 2
0
        private static Image?GetPictureFromParams(
            int handle,
            PICTYPE type,
            int paletteHandle,
            int width,
            int height)
        {
            switch (type)
            {
            case PICTYPE.ICON:
                return((Image)Icon.FromHandle((IntPtr)handle).Clone());

            case PICTYPE.METAFILE:
                WmfPlaceableFileHeader header = new WmfPlaceableFileHeader
                {
                    BboxRight  = (short)width,
                    BboxBottom = (short)height
                };

                using (var metafile = new Metafile((IntPtr)handle, header, deleteWmf: false))
                {
                    return((Image)metafile.Clone());
                }

            case PICTYPE.ENHMETAFILE:
                using (var metafile = new Metafile((IntPtr)handle, deleteEmf: false))
                {
                    return((Image)metafile.Clone());
                }

            case PICTYPE.BITMAP:
                return(Image.FromHbitmap((IntPtr)handle, (IntPtr)paletteHandle));

            case PICTYPE.NONE:
                // MSDN says this should not be a valid value, but comctl32 returns it...
                return(null);

            case PICTYPE.UNINITIALIZED:
                return(null);

            default:
                throw new ArgumentException("AXUnknownImage", nameof(type));
            }
        }
Ejemplo n.º 3
0
        public static Image?GetPictureFromIPicture(object picture)
        {
            int      hPal = default;
            IPicture pict = (IPicture)picture;
            PICTYPE  type = (PICTYPE)pict.Type;

            if (type == PICTYPE.BITMAP)
            {
                try
                {
                    hPal = pict.hPal;
                }
                catch (COMException)
                {
                }
            }

            return(GetPictureFromParams(pict.Handle, type, hPal, pict.Width, pict.Height));
        }