/// <summary> /// Loads a picture from the requested stream. /// </summary> private void LoadPicture(UnsafeNativeMethods.IStream stream) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } try { Guid g = typeof(UnsafeNativeMethods.IPicture).GUID; UnsafeNativeMethods.IPicture picture = null; 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.CopyImage( 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 { // 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); }
/// <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); handle = SafeNativeMethods.CopyImageAsCursor(new HandleRef(this, cursorHandle), NativeMethods.IMAGE_CURSOR, picSize.Width, picSize.Height, 0); ownHandle = true; } else { throw new ArgumentException(SR.GetString(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.GetString(SR.InvalidPictureFormat), "stream", e); } }