public override object ConvertManagedToNative(object managedValue, Com2PropertyDescriptor pd, ref bool cancelSet)
 {
     cancelSet = false;
     if (((this.lastManaged != null) && this.lastManaged.Equals(managedValue)) && ((this.pictureRef != null) && this.pictureRef.IsAlive))
     {
         return(this.pictureRef.Target);
     }
     this.lastManaged = managedValue;
     if (managedValue != null)
     {
         Guid gUID = typeof(System.Windows.Forms.UnsafeNativeMethods.IPicture).GUID;
         System.Windows.Forms.NativeMethods.PICTDESC pictdesc = null;
         bool fOwn = false;
         if (this.lastManaged is Icon)
         {
             pictdesc = System.Windows.Forms.NativeMethods.PICTDESC.CreateIconPICTDESC(((Icon)this.lastManaged).Handle);
         }
         else if (this.lastManaged is Bitmap)
         {
             pictdesc = System.Windows.Forms.NativeMethods.PICTDESC.CreateBitmapPICTDESC(((Bitmap)this.lastManaged).GetHbitmap(), this.lastPalette);
             fOwn     = true;
         }
         System.Windows.Forms.UnsafeNativeMethods.IPicture target = System.Windows.Forms.UnsafeNativeMethods.OleCreatePictureIndirect(pictdesc, ref gUID, fOwn);
         this.lastNativeHandle = target.GetHandle();
         this.pictureRef       = new WeakReference(target);
         return(target);
     }
     this.lastManaged      = null;
     this.lastNativeHandle = this.lastPalette = IntPtr.Zero;
     this.pictureRef       = null;
     return(null);
 }
Ejemplo n.º 2
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);
        }