Esempio n. 1
0
        /// <include file='doc\Bitmap.uex' path='docs/doc[@for="Bitmap.MakeTransparent1"]/*' />
        /// <devdoc>
        ///    Makes the specified color transparent
        ///    for this <see cref='System.Drawing.Bitmap'/>.
        /// </devdoc>
        public void MakeTransparent(Color transparentColor)
        {
            if (RawFormat.Guid == ImageFormat.Icon.Guid)
            {
                throw new InvalidOperationException(SR.GetString(SR.CantMakeIconTransparent));
            }

            Size        size   = Size;
            PixelFormat format = PixelFormat;

            // The new bitmap must be in 32bppARGB  format, because that's the only
            // thing that supports alpha.  (And that's what the image is initialized to -- transparent)
            Bitmap   result   = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb);
            Graphics graphics = Graphics.FromImage(result);

            graphics.Clear(Color.Transparent);
            Rectangle rectangle = new Rectangle(0, 0, size.Width, size.Height);

            ImageAttributes attributes = new ImageAttributes();

            attributes.SetColorKey(transparentColor, transparentColor);
            graphics.DrawImage(this, rectangle,
                               0, 0, size.Width, size.Height,
                               GraphicsUnit.Pixel, attributes, null, IntPtr.Zero);
            attributes.Dispose();
            graphics.Dispose();

            // Swap nativeImage pointers to make it look like we modified the image in place
            IntPtr temp = this.nativeImage;

            this.nativeImage   = result.nativeImage;
            result.nativeImage = temp;
            result.Dispose();
        }
Esempio n. 2
0
        public static void DrawImageTransparent(this Graphics gx, Image image, Rectangle destRect, Color transpColor)
        {
            ImageAttributes imageAttr = new ImageAttributes();

            imageAttr.SetColorKey(transpColor, transpColor);
            gx.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr);
            imageAttr.Dispose();
        }
Esempio n. 3
0
        public void MakeTransparent(Color transparentColor)
        {
            // We have to draw always over a 32-bitmap surface that supports alpha channel
            Bitmap          bmp       = new Bitmap(Width, Height, PixelFormat.Format32bppArgb);
            Graphics        gr        = Graphics.FromImage(bmp);
            Rectangle       destRect  = new Rectangle(0, 0, Width, Height);
            ImageAttributes imageAttr = new ImageAttributes();

            imageAttr.SetColorKey(transparentColor, transparentColor);

            gr.DrawImage(this, destRect, 0, 0, Width, Height, GraphicsUnit.Pixel, imageAttr);

            IntPtr oldBmp = nativeObject;

            nativeObject     = bmp.nativeObject;
            bmp.nativeObject = oldBmp;

            gr.Dispose();
            bmp.Dispose();
            imageAttr.Dispose();
        }
Esempio n. 4
0
        public void MakeTransparent(Color transparentColor)
        {
            if (base.RawFormat.Guid == ImageFormat.Icon.Guid)
            {
                throw new InvalidOperationException(System.Drawing.SR.GetString("CantMakeIconTransparent"));
            }
            Size     size     = base.Size;
            Bitmap   image    = null;
            Graphics graphics = null;

            try
            {
                image = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb);
                using (graphics = Graphics.FromImage(image))
                {
                    graphics.Clear(Color.Transparent);
                    Rectangle destRect = new Rectangle(0, 0, size.Width, size.Height);
                    using (ImageAttributes attributes = null)
                    {
                        attributes = new ImageAttributes();
                        attributes.SetColorKey(transparentColor, transparentColor);
                        graphics.DrawImage(this, destRect, 0, 0, size.Width, size.Height, GraphicsUnit.Pixel, attributes, null, IntPtr.Zero);
                    }
                }
                IntPtr nativeImage = base.nativeImage;
                base.nativeImage  = image.nativeImage;
                image.nativeImage = nativeImage;
            }
            finally
            {
                if (image != null)
                {
                    image.Dispose();
                }
            }
        }