/// <devdoc> /// Initializes this image for the first time. This should only be called once. /// </devdoc> private void Initialize(SafeNativeMethods.IPicture picture) { if (this.picture != null) { throw new InvalidOperationException(SR.GetString(SR.IllegalState, GetType().Name)); } this.picture = picture; // SECUNDONE : Assert shouldn't be needed, however we can't put SuppressUnmanagedCode // : on the IPicture interface, so we must do an Assert. // new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert(); try { if (picture != null && picture.GetPictureType() == NativeMethods.Ole.PICTYPE_ICON) { handle = picture.GetHandle(); ownHandle = false; } else { throw new ArgumentException(SR.GetString(SR.InvalidPictureType, "picture", "Cursor"), "picture"); } } finally { CodeAccessPermission.RevertAssert(); } }
/// <include file='doc\COM2PictureConverter.uex' path='docs/doc[@for="Com2PictureConverter.ConvertNativeToManaged"]/*' /> /// <devdoc> /// Converts the native value into a managed value /// </devdoc> public override object ConvertNativeToManaged(object nativeValue, Com2PropertyDescriptor pd) { if (nativeValue == null) { return(null); } Debug.Assert(nativeValue is SafeNativeMethods.IPicture, "nativevalue is not IPicture"); SafeNativeMethods.IPicture nativePicture = (SafeNativeMethods.IPicture)nativeValue; IntPtr handle = nativePicture.GetHandle(); if (lastManaged != null && handle == lastNativeHandle) { return(lastManaged); } lastNativeHandle = handle; //lastPalette = nativePicture.GetHPal(); if (handle != IntPtr.Zero) { switch (nativePicture.GetPictureType()) { case NativeMethods.Ole.PICTYPE_ICON: pictureType = typeof(Icon); lastManaged = Icon.FromHandle(handle); break; case NativeMethods.Ole.PICTYPE_BITMAP: pictureType = typeof(Bitmap); lastManaged = Image.FromHbitmap(handle); break; default: Debug.Fail("Unknown picture type"); break; } pictureRef = new WeakReference(nativePicture); } else { lastManaged = null; pictureRef = null; } return(lastManaged); }
/// <include file='doc\COM2PictureConverter.uex' path='docs/doc[@for="Com2PictureConverter.ConvertManagedToNative"]/*' /> /// <devdoc> /// Converts the managed value into a native value /// </devdoc> public override object ConvertManagedToNative(object managedValue, Com2PropertyDescriptor pd, ref bool cancelSet) { // don't cancel the set cancelSet = false; if (lastManaged != null && lastManaged.Equals(managedValue) && pictureRef != null && pictureRef.IsAlive) { return(pictureRef.Target); } // we have to build an IPicture lastManaged = managedValue; if (managedValue != null) { Guid g = typeof(SafeNativeMethods.IPicture).GUID; NativeMethods.PICTDESC pictdesc = null; bool own = false; if (lastManaged is Icon) { pictdesc = NativeMethods.PICTDESC.CreateIconPICTDESC(((Icon)lastManaged).Handle); } else if (lastManaged is Bitmap) { pictdesc = NativeMethods.PICTDESC.CreateBitmapPICTDESC(((Bitmap)lastManaged).GetHbitmap(), lastPalette); own = true; } else { Debug.Fail("Unknown Image type: " + managedValue.GetType().Name); } SafeNativeMethods.IPicture pict = SafeNativeMethods.OleCreatePictureIndirect(pictdesc, ref g, own); lastNativeHandle = pict.GetHandle(); pictureRef = new WeakReference(pict); return(pict); } else { lastManaged = null; lastNativeHandle = lastPalette = IntPtr.Zero; pictureRef = null; return(null); } }