Пример #1
0
        /// <devdoc>
        ///     Loads an image from a COM stream.  This will return a bitmap, icon,
        ///     cursor or metafile object depending on the type of data in the stream.
        /// </devdoc>
        unsafe private static Cursor LoadImage(UnsafeNativeMethods.IStream stream)
        {
            Cursor cursor = null;

            SafeNativeMethods.IPicture picture = LoadPicture(stream);
            if (picture.GetPictureType() == NativeMethods.Ole.PICTYPE_ICON)
            {
                // black magic stolen from oleaut.
                stream.Seek(2, NativeMethods.STREAM_SEEK_SET);
                byte b       = 0;
                int  numRead = stream.Read((IntPtr)(int)&b, 1);

                Debug.Assert(numRead == 1, "Could read 1 byte from the stream!!!");

                if (numRead == 1 && b == 2)
                {
                    cursor = new Cursor(picture);
                }
                // Otherwise, it's really an Icon.
            }

            if (cursor != null)
            {
                return(cursor);
            }
            else
            {
                throw new ArgumentException(SR.GetString(SR.InvalidPictureType,
                                                         "picture",
                                                         "Cursor"), "stream");
            }
        }
Пример #2
0
        /// <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();
            }
        }
Пример #3
0
        /// <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);
        }