Esempio n. 1
0
            public Image this[int index] {
                get {
                    if (index < 0 || index >= Count)
                    {
                        throw new ArgumentOutOfRangeException(SR.GetString(SR.InvalidArgument,
                                                                           "index",
                                                                           index.ToString()));
                    }
                    return(owner.GetBitmap(index));
                }
                set {
                    if (index < 0 || index >= Count)
                    {
                        throw new ArgumentOutOfRangeException(SR.GetString(SR.InvalidArgument,
                                                                           "index",
                                                                           index.ToString()));
                    }

                    if (value == null)
                    {
                        throw new ArgumentNullException("value");
                    }

                    if (!(value is Bitmap))
                    {
                        throw new ArgumentException(SR.GetString(SR.ImageListBitmap));
                    }


                    AssertInvariant();

                    Bitmap bitmap = (Bitmap)value;

                    if (owner.UseTransparentColor)
                    {
                        // Since there's no ImageList_ReplaceMasked, we need to generate
                        // a transparent bitmap
                        Bitmap source = bitmap;
                        bitmap = (Bitmap)bitmap.Clone();
                        bitmap.MakeTransparent(owner.transparentColor);
                    }


                    IntPtr hMask   = ControlPaint.CreateHBitmapTransparencyMask(bitmap);
                    IntPtr hBitmap = ControlPaint.CreateHBitmapColorMask(bitmap, hMask);
                    bool   ok      = SafeNativeMethods.ImageList_Replace(new HandleRef(owner, owner.Handle), index, new HandleRef(null, hBitmap), new HandleRef(null, hMask));
                    SafeNativeMethods.DeleteObject(new HandleRef(null, hBitmap));
                    SafeNativeMethods.DeleteObject(new HandleRef(null, hMask));

                    if (!ok)
                    {
                        throw new InvalidOperationException(SR.GetString(SR.ImageListReplaceFailed));
                    }
                }
            }
 public Image this[int index]
 {
     get
     {
         if ((index < 0) || (index >= this.Count))
         {
             throw new ArgumentOutOfRangeException("index", System.Windows.Forms.SR.GetString("InvalidArgument", new object[] { "index", index.ToString(CultureInfo.CurrentCulture) }));
         }
         return(this.owner.GetBitmap(index));
     }
     set
     {
         if ((index < 0) || (index >= this.Count))
         {
             throw new ArgumentOutOfRangeException("index", System.Windows.Forms.SR.GetString("InvalidArgument", new object[] { "index", index.ToString(CultureInfo.CurrentCulture) }));
         }
         if (value == null)
         {
             throw new ArgumentNullException("value");
         }
         if (!(value is Bitmap))
         {
             throw new ArgumentException(System.Windows.Forms.SR.GetString("ImageListBitmap"));
         }
         Bitmap bitmap = (Bitmap)value;
         bool   flag   = false;
         if (this.owner.UseTransparentColor)
         {
             bitmap = (Bitmap)bitmap.Clone();
             bitmap.MakeTransparent(this.owner.transparentColor);
             flag = true;
         }
         try
         {
             IntPtr monochromeMask = ControlPaint.CreateHBitmapTransparencyMask(bitmap);
             IntPtr handle         = ControlPaint.CreateHBitmapColorMask(bitmap, monochromeMask);
             bool   flag2          = System.Windows.Forms.SafeNativeMethods.ImageList_Replace(new HandleRef(this.owner, this.owner.Handle), index, new HandleRef(null, handle), new HandleRef(null, monochromeMask));
             System.Windows.Forms.SafeNativeMethods.DeleteObject(new HandleRef(null, handle));
             System.Windows.Forms.SafeNativeMethods.DeleteObject(new HandleRef(null, monochromeMask));
             if (!flag2)
             {
                 throw new InvalidOperationException(System.Windows.Forms.SR.GetString("ImageListReplaceFailed"));
             }
         }
         finally
         {
             if (flag)
             {
                 bitmap.Dispose();
             }
         }
     }
 }
        private int AddToHandle(Original original, Bitmap bitmap)
        {
            IntPtr monochromeMask = ControlPaint.CreateHBitmapTransparencyMask(bitmap);
            IntPtr handle         = ControlPaint.CreateHBitmapColorMask(bitmap, monochromeMask);
            int    num            = System.Windows.Forms.SafeNativeMethods.ImageList_Add(new HandleRef(this, this.Handle), new HandleRef(null, handle), new HandleRef(null, monochromeMask));

            System.Windows.Forms.SafeNativeMethods.DeleteObject(new HandleRef(null, handle));
            System.Windows.Forms.SafeNativeMethods.DeleteObject(new HandleRef(null, monochromeMask));
            if (num == -1)
            {
                throw new InvalidOperationException(System.Windows.Forms.SR.GetString("ImageListAddFailed"));
            }
            return(num);
        }
Esempio n. 4
0
        // Adds bitmap to the Imagelist handle...
        //
        private int AddToHandle(Original original, Bitmap bitmap)
        {
            Debug.Assert(HandleCreated, "Calling AddToHandle when there is no handle");
            IntPtr hMask   = ControlPaint.CreateHBitmapTransparencyMask(bitmap);
            IntPtr hBitmap = ControlPaint.CreateHBitmapColorMask(bitmap, hMask);
            int    index   = SafeNativeMethods.ImageList_Add(new HandleRef(this, Handle), new HandleRef(null, hBitmap), new HandleRef(null, hMask));

            SafeNativeMethods.DeleteObject(new HandleRef(null, hBitmap));
            SafeNativeMethods.DeleteObject(new HandleRef(null, hMask));

            if (index == -1)
            {
                throw new InvalidOperationException(SR.GetString(SR.ImageListAddFailed));
            }
            return(index);
        }