Exemplo n.º 1
0
 public void Save(Stream outputStream)
 {
     if (this.iconData != null)
     {
         outputStream.Write(this.iconData, 0, this.iconData.Length);
     }
     else
     {
         SafeNativeMethods.PICTDESC pictdesc = SafeNativeMethods.PICTDESC.CreateIconPICTDESC(this.Handle);
         Guid gUID = typeof(SafeNativeMethods.IPicture).GUID;
         SafeNativeMethods.IPicture o = SafeNativeMethods.OleCreatePictureIndirect(pictdesc, ref gUID, false);
         if (o != null)
         {
             try
             {
                 int num;
                 o.SaveAsFile(new System.Drawing.UnsafeNativeMethods.ComStreamFromDataStream(outputStream), -1, out num);
             }
             finally
             {
                 Marshal.ReleaseComObject(o);
             }
         }
     }
 }
Exemplo n.º 2
0
        public void Save(Stream outputStream)
        {
            if (_iconData != null)
            {
                outputStream.Write(_iconData, 0, _iconData.Length);
            }
            else
            {
                // Ideally, we would pick apart the icon using
                // GetIconInfo, and then pull the individual bitmaps out,
                // converting them to DIBS and saving them into the file.
                // But, in the interest of simplicity, we just call to
                // OLE to do it for us.
                SafeNativeMethods.PICTDESC pictdesc = SafeNativeMethods.PICTDESC.CreateIconPICTDESC(Handle);
                Guid g = typeof(SafeNativeMethods.IPicture).GUID;
                SafeNativeMethods.IPicture picture = SafeNativeMethods.OleCreatePictureIndirect(pictdesc, ref g, false);

                if (picture != null)
                {
                    try
                    {
                        picture.SaveAsFile(new UnsafeNativeMethods.ComStreamFromDataStream(outputStream), -1, out int temp);
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(picture);
                    }
                }
            }
        }
Exemplo n.º 3
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");
            }
        }
Exemplo n.º 4
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();
            }
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        /// <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);
            }
        }
Exemplo n.º 7
0
 /// <devdoc>
 ///     Loads a picture from the requested stream.
 /// </devdoc>
 private static SafeNativeMethods.IPicture LoadPicture(UnsafeNativeMethods.IStream stream)
 {
     SafeNativeMethods.IPicture picture = null;
     if (stream == null)
     {
         throw new ArgumentException(SR.GetString(SR.InvalidArgument, "stream", "null"));
     }
     try {
         Guid g = typeof(SafeNativeMethods.IPicture).GUID;
         picture = SafeNativeMethods.OleLoadPicture(stream, 0, false, ref g);
     }
     catch (Exception e) {
         Debug.Fail(e.ToString());
         throw new ArgumentException(SR.GetString(SR.InvalidPictureFormat), "stream");
     }
     return(picture);
 }
Exemplo n.º 8
0
        void Dispose(bool disposing)
        {
            if (picture != null)
            {
                picture = null;

                // If we have no message loop, OLE may block on this call.
                // Let pent up SendMessage calls go through here.
                //
                NativeMethods.MSG msg = new NativeMethods.MSG();
                UnsafeNativeMethods.PeekMessage(ref msg, NativeMethods.NullHandleRef, 0, 0, NativeMethods.PM_NOREMOVE | NativeMethods.PM_NOYIELD);
            }

            if (handle != IntPtr.Zero)
            {
                DestroyHandle();
                handle = IntPtr.Zero;
            }
        }
Exemplo n.º 9
0
 /// <devdoc>
 ///    <para>
 ///       Initializes a new instance of the <see cref='System.Windows.Forms.Cursor'/> class from a
 ///       COM stream.
 ///    </para>
 /// </devdoc>
 private Cursor(SafeNativeMethods.IPicture picture)
 {
     Initialize(picture);
 }