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));
                    }
                }
            }