Пример #1
0
 private void LoadPicture(System.Windows.Forms.UnsafeNativeMethods.IStream stream)
 {
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     try
     {
         Guid gUID = typeof(System.Windows.Forms.UnsafeNativeMethods.IPicture).GUID;
         System.Windows.Forms.UnsafeNativeMethods.IPicture o = null;
         new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert();
         try
         {
             o = System.Windows.Forms.UnsafeNativeMethods.OleCreateIPictureIndirect(null, ref gUID, true);
             ((System.Windows.Forms.UnsafeNativeMethods.IPersistStream)o).Load(stream);
             if ((o == null) || (o.GetPictureType() != 3))
             {
                 throw new ArgumentException(System.Windows.Forms.SR.GetString("InvalidPictureType", new object[] { "picture", "Cursor" }), "picture");
             }
             IntPtr handle = o.GetHandle();
             System.Drawing.Size iconSize = this.GetIconSize(handle);
             this.handle    = System.Windows.Forms.SafeNativeMethods.CopyImageAsCursor(new HandleRef(this, handle), 2, iconSize.Width, iconSize.Height, 0);
             this.ownHandle = true;
         }
         finally
         {
             CodeAccessPermission.RevertAssert();
             if (o != null)
             {
                 Marshal.ReleaseComObject(o);
             }
         }
     }
     catch (COMException exception)
     {
         throw new ArgumentException(System.Windows.Forms.SR.GetString("InvalidPictureFormat"), "stream", exception);
     }
 }
        public override object ConvertNativeToManaged(object nativeValue, Com2PropertyDescriptor pd)
        {
            if (nativeValue == null)
            {
                return(null);
            }
            System.Windows.Forms.UnsafeNativeMethods.IPicture target = (System.Windows.Forms.UnsafeNativeMethods.IPicture)nativeValue;
            IntPtr handle = target.GetHandle();

            if ((this.lastManaged == null) || (handle != this.lastNativeHandle))
            {
                this.lastNativeHandle = handle;
                if (!(handle != IntPtr.Zero))
                {
                    this.lastManaged = null;
                    this.pictureRef  = null;
                }
                else
                {
                    switch (target.GetPictureType())
                    {
                    case 1:
                        this.pictureType = typeof(Bitmap);
                        this.lastManaged = Image.FromHbitmap(handle);
                        break;

                    case 3:
                        this.pictureType = typeof(Icon);
                        this.lastManaged = Icon.FromHandle(handle);
                        break;
                    }
                    this.pictureRef = new WeakReference(target);
                }
            }
            return(this.lastManaged);
        }