public static IntPtr ImageList_Read(UnsafeNativeMethods.IStream pstm)
        {
            IntPtr newHandle = IntImageList_Read(pstm);

            validImageListHandles.Add(newHandle);
            return(newHandle);
        }
Exemple #2
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");
            }
        }
Exemple #3
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.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);
            }
        }
Exemple #4
0
 public override void Close()
 {
     if (comStream != null)
     {
         try {
             comStream.Commit(NativeMethods.STGC_DEFAULT);
         }
         catch (Exception) {
         }
         comStream = null;
     }
 }
Exemple #5
0
        public virtual long CopyTo(UnsafeNativeMethods.IStream pstm, long cb, long[] pcbRead)
        {
            int    bufsize = 4096; // one page
            IntPtr buffer  = Marshal.AllocHGlobal(bufsize);

            if (buffer == IntPtr.Zero)
            {
                throw new OutOfMemoryException();
            }

            long written = 0;

            try
            {
                while (written < cb)
                {
                    int toRead = bufsize;
                    if (written + toRead > cb)
                    {
                        toRead = (int)(cb - written);
                    }

                    int read = Read(buffer, toRead);
                    if (read == 0)
                    {
                        break;
                    }

                    if (pstm.Write(buffer, read) != read)
                    {
                        throw EFail("Wrote an incorrect number of bytes");
                    }

                    written += read;
                }
            }
            finally
            {
                Marshal.FreeHGlobal(buffer);
            }
            if (pcbRead != null && pcbRead.Length > 0)
            {
                pcbRead[0] = written;
            }

            return(written);
        }
Exemple #6
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);
            }
        }
Exemple #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);
 }
 protected override void Dispose(bool disposing)
 {
     try {
         if (disposing && comStream != null)
         {
             try {
                 comStream.Commit(NativeMethods.STGC_DEFAULT);
             }
             catch (Exception) {
             }
         }
         // Can't release a COM stream from the finalizer thread.
         comStream = null;
     }
     finally {
         base.Dispose(disposing);
     }
 }
Exemple #9
0
 protected override void Dispose(bool disposing)
 {
     try
     {
         if (disposing && (this.comStream != null))
         {
             try
             {
                 this.comStream.Commit(0);
             }
             catch (Exception)
             {
             }
         }
         this.comStream = null;
     }
     finally
     {
         base.Dispose(disposing);
     }
 }
 protected override void Dispose(bool disposing)
 {
     try
     {
         if (disposing && (this.comStream != null))
         {
             try
             {
                 this.comStream.Commit(0);
             }
             catch (Exception)
             {
             }
         }
         this.comStream = null;
     }
     finally
     {
         base.Dispose(disposing);
     }
 }
 public DataStreamFromComStream(UnsafeNativeMethods.IStream comStream) : base()
 {
     this.comStream = comStream;
 }
 public static extern int ImageList_WriteEx(HandleRef himl, int dwFlags, UnsafeNativeMethods.IStream pstm);
 public static extern bool ImageList_Write(HandleRef himl, UnsafeNativeMethods.IStream pstm);
 public static extern IntPtr ImageList_Read(UnsafeNativeMethods.IStream pstm);
 private static extern IntPtr IntImageList_Read(UnsafeNativeMethods.IStream pstm);
 public DataStreamFromComStream(UnsafeNativeMethods.IStream comStream)
 {
     this.comStream = comStream;
 }
 protected override void Dispose(bool disposing) {
     try {               
         if (disposing && comStream != null) {
             try {
                 comStream.Commit(NativeMethods.STGC_DEFAULT);
             }
             catch(Exception) {
             }
         }
         // Can't release a COM stream from the finalizer thread.
         comStream = null;
     }
     finally {
         base.Dispose(disposing);
     }
 }