Пример #1
0
        /// <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.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
                {
                    // destroy the picture...
                    if (picture != null)
                    {
                        Marshal.ReleaseComObject(picture);
                    }
                }
            }
            catch (COMException e)
            {
                Debug.Fail(e.ToString());
                throw new ArgumentException(SR.InvalidPictureFormat, "stream", e);
            }
        }
Пример #2
0
        /// <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);
            }
        }