/// <devdoc> /// Loads a picture from the requested stream. /// </devdoc> private void LoadPicture(UnsafeNativeMethods.IStream stream) { if (stream == null) { throw new ArgumentNullException("stream"); } try { Guid g = typeof(UnsafeNativeMethods.IPicture).GUID; UnsafeNativeMethods.IPicture picture = null; new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert(); try { picture = UnsafeNativeMethods.OleCreateIPictureIndirect(null, ref g, true); UnsafeNativeMethods.IPersistStream ipictureAsIPersist = (UnsafeNativeMethods.IPersistStream)picture; ipictureAsIPersist.Load(stream); if (picture != null && picture.GetPictureType() == NativeMethods.Ole.PICTYPE_ICON) { IntPtr cursorHandle = picture.GetHandle(); Size picSize = GetIconSize(cursorHandle); if (DpiHelper.IsScalingRequired) { picSize = DpiHelper.LogicalToDeviceUnits(picSize); } handle = SafeNativeMethods.CopyImageAsCursor(new HandleRef(this, cursorHandle), NativeMethods.IMAGE_CURSOR, picSize.Width, picSize.Height, 0); ownHandle = true; } else { throw new ArgumentException(string.Format(SR.InvalidPictureType, "picture", "Cursor"), "picture"); } } finally { CodeAccessPermission.RevertAssert(); // destroy the picture... if (picture != null) { Marshal.ReleaseComObject(picture); } } } catch (COMException e) { Debug.Fail(e.ToString()); throw new ArgumentException(SR.InvalidPictureFormat, "stream", e); } }
public override object ConvertNativeToManaged(object nativeValue, Com2PropertyDescriptor pd) { if (nativeValue == null) { return(null); } Debug.Assert(nativeValue is UnsafeNativeMethods.IPicture, "nativevalue is not IPicture"); UnsafeNativeMethods.IPicture nativePicture = (UnsafeNativeMethods.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); }
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(UnsafeNativeMethods.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); } UnsafeNativeMethods.IPicture pict = UnsafeNativeMethods.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); } }