Example #1
0
        public static Image GetDisabledImage(Image originalBitmap)
        {
            var disabledBitmap = new Bitmap(originalBitmap.Width, originalBitmap.Height);
            var g = Graphics.FromImage(disabledBitmap);

            // We create a color matrix to generated the disabled look
            var array = new float[5][];

            array[0] = new float[] { 0.2125f, 0.2125f, 0.2125f, 0, 0 };
            array[1] = new float[] { 0.2577f, 0.2577f, 0.2577f, 0, 0 };
            array[2] = new float[] { 0.0361f, 0.0361f, 0.0361f, 0, 0 };
            array[3] = new float[] { 0, 0, 0, 1, 0 };
            array[4] = new float[] { 0.38f, 0.38f, 0.38f, 0, 1 };
            var grayMatrix        = new ColorMatrix(array);
            var disabledImageAttr = new ImageAttributes();

            disabledImageAttr.ClearColorKey();
            disabledImageAttr.SetColorMatrix(grayMatrix);

            // We draw the image using the color matrix
            var rectImage = new Rectangle(0, 0, originalBitmap.Width, originalBitmap.Height);

            g.DrawImage(originalBitmap, rectImage, 0, 0, originalBitmap.Width, originalBitmap.Height, GraphicsUnit.Pixel, disabledImageAttr);
            g.Dispose();

            return(disabledBitmap);
        }
Example #2
0
        /// <summary>
        ///     Creates the disabled image for the specified Image
        /// </summary>
        /// <param name="normalImage"></param>
        /// <returns></returns>
        public static Image CreateDisabledImage(Image normalImage)
        {
            var imageAttr = new ImageAttributes();

            imageAttr.ClearColorKey();
            imageAttr.SetColorMatrix(DisabledImageColorMatrix);
            var size     = normalImage.Size;
            var image    = new Bitmap(size.Width, size.Height);
            var graphics = Graphics.FromImage(image);

            graphics.DrawImage(normalImage, new Rectangle(0, 0, size.Width, size.Height), 0, 0, size.Width, size.Height, GraphicsUnit.Pixel, imageAttr);
            graphics.Dispose();
            return(image);
        }
Example #3
0
        private void DrawAlphaImage(Image alphaImage, Image image, float percent)
        {
            float[][] newColorMatrix = new float[5][];
            newColorMatrix[0] = new float[] { 1, 0, 0, 0, 0 };
            newColorMatrix[1] = new float[] { 0, 1, 0, 0, 0 };
            newColorMatrix[2] = new float[] { 0, 0, 1, 0, 0 };
            newColorMatrix[3] = new float[] { 0, 0, 0, percent, 0 };
            newColorMatrix[4] = new float[] { 0, 0, 0, 0, 1 };
            ColorMatrix     matrix          = new ColorMatrix(newColorMatrix);
            ImageAttributes imageAttributes = new ImageAttributes();

            imageAttributes.ClearColorKey();
            imageAttributes.SetColorMatrix(matrix);
            using (Graphics gr = Graphics.FromImage(alphaImage))
            {
                gr.DrawImage(image, new Rectangle(0, 0, Size.Width, Size.Height), 0, 0, Size.Width, Size.Height, GraphicsUnit.Pixel, imageAttributes);
            }
        }
        /*
         * GetGrayscaleImageAttributes
         */

        /// <summary>
        /// </summary>
        /// <returns></returns>
        public static ImageAttributes GetGrayscaleImageAttributes()
        {
            float[][] matrixComponents = new float[5][];
            matrixComponents[0] = new float[] { 0.2125f, 0.2125f, 0.2125f, 0f, 0f };
            matrixComponents[1] = new float[] { 0.2577f, 0.2577f, 0.2577f, 0f, 0f };
            matrixComponents[2] = new float[] { 0.0361f, 0.0361f, 0.0361f, 0f, 0f };

            float[] transformComponents = new float[5];
            transformComponents[3] = 1f;
            matrixComponents[3]    = transformComponents;
            matrixComponents[4]    = new float[] { 0.38f, 0.38f, 0.38f, 0f, 1f };

            ImageAttributes imageAttributes = new ImageAttributes();

            imageAttributes.ClearColorKey();
            imageAttributes.SetColorMatrix(new ColorMatrix(matrixComponents));

            return(imageAttributes);
        }
Example #5
0
        /// <summary>
        /// Create a disabled version of the image.
        /// </summary>
        /// <param name="image">The image to convert</param>
        /// <param name="background">The Color of the background behind the image. The background parameter is used to calculate the fill color of the disabled image so that it is always visible against the background.</param>
        /// <returns></returns>
        public static Image CreateDisabledImage(Image image, Color background)
        {
            if (image == null)
            {
                return(null);
            }

            Size imgSize = image.Size;

            if (_sDisabledImageAttr == null)
            {
                float[][] arrayJagged = new float[5][];
                arrayJagged[0] = new float[5] {
                    0.2125f, 0.2125f, 0.2125f, 0f, 0f
                };
                arrayJagged[1] = new float[5] {
                    0.2577f, 0.2577f, 0.2577f, 0f, 0f
                };
                arrayJagged[2] = new float[5] {
                    0.0361f, 0.0361f, 0.0361f, 0f, 0f
                };
                float[] arraySingle = new float[5];
                arraySingle[3] = 1f;
                arrayJagged[3] = arraySingle;
                arrayJagged[4] = new float[5] {
                    0.38f, 0.38f, 0.38f, 0f, 1f
                };
                System.Drawing.Imaging.ColorMatrix matrix = new System.Drawing.Imaging.ColorMatrix(arrayJagged);
                _sDisabledImageAttr = new System.Drawing.Imaging.ImageAttributes();
                _sDisabledImageAttr.ClearColorKey();

                _sDisabledImageAttr.SetColorMatrix(matrix);
            }

            Bitmap bitmap = new Bitmap(image.Width, image.Height);

            using (Graphics g = Graphics.FromImage(bitmap)) {
                g.DrawImage(image, new Rectangle(0, 0, imgSize.Width, imgSize.Height), 0, 0, imgSize.Width, imgSize.Height, GraphicsUnit.Pixel, _sDisabledImageAttr);
            }

            return(bitmap);
        }
Example #6
0
        /// <include file='doc\ToolStripRenderer.uex' path='docs/doc[@for="ToolStripRenderer.CreateDisabledImage"]/*' />
        public static Image CreateDisabledImage(Image normalImage)
        {
            ImageAttributes imgAttrib = new ImageAttributes();

            imgAttrib.ClearColorKey();
            imgAttrib.SetColorMatrix(DisabledImageColorMatrix);

            Size     size           = normalImage.Size;
            Bitmap   disabledBitmap = new Bitmap(size.Width, size.Height);
            Graphics graphics       = Graphics.FromImage(disabledBitmap);

            graphics.DrawImage(normalImage,
                               new Rectangle(0, 0, size.Width, size.Height),
                               0, 0, size.Width, size.Height,
                               GraphicsUnit.Pixel,
                               imgAttrib);
            graphics.Dispose();

            return(disabledBitmap);
        }
Example #7
0
        /// <devdoc>
        ///     Draws an image and makes it look disabled.
        /// </devdoc>
        internal static void DrawImageDisabled(Graphics graphics, Image image, Rectangle imageBounds, Color background, bool unscaledImage)
        {
            Size imageSize = image.Size;

            if (disabledImageAttr == null)
            {
                var array = new float[5][];
                array[0] = new[] { 0.2125f, 0.2125f, 0.2125f, 0, 0 };
                array[1] = new[] { 0.2577f, 0.2577f, 0.2577f, 0, 0 };
                array[2] = new[] { 0.0361f, 0.0361f, 0.0361f, 0, 0 };
                array[3] = new[] { 0f, 0f, 0f, 1f, 0f };
                array[4] = new[] { 0.38f, 0.38f, 0.38f, 0, 1 };
                ColorMatrix grayMatrix = new ColorMatrix(array);
                disabledImageAttr = new ImageAttributes();
                disabledImageAttr.ClearColorKey();
                disabledImageAttr.SetColorMatrix(grayMatrix);
            }

            if (unscaledImage)
            {
                using (Bitmap bmp = new Bitmap(image.Width, image.Height)) {
                    using (Graphics g = Graphics.FromImage(bmp)) {
                        g.DrawImage(image,
                                    new Rectangle(0, 0, imageSize.Width, imageSize.Height),
                                    0, 0, imageSize.Width, imageSize.Height,
                                    GraphicsUnit.Pixel,
                                    disabledImageAttr);
                    }

                    graphics.DrawImageUnscaled(bmp, imageBounds);
                }
            }
            else
            {
                graphics.DrawImage(image,
                                   imageBounds,
                                   0, 0, imageSize.Width, imageSize.Height,
                                   GraphicsUnit.Pixel,
                                   disabledImageAttr);
            }
        }
Example #8
0
        internal static Image CreateDisabledImage(Image normalImage)
        {
            ArgumentNullException.ThrowIfNull(normalImage);

            ImageAttributes imgAttrib = new ImageAttributes();

            imgAttrib.ClearColorKey();
            imgAttrib.SetColorMatrix(DisabledImageColorMatrix);

            Size   size           = normalImage.Size;
            Bitmap disabledBitmap = new Bitmap(size.Width, size.Height);

            using (Graphics graphics = Graphics.FromImage(disabledBitmap))
            {
                graphics.DrawImage(normalImage,
                                   new Rectangle(0, 0, size.Width, size.Height),
                                   0, 0, size.Width, size.Height,
                                   GraphicsUnit.Pixel,
                                   imgAttrib);
            }

            return(disabledBitmap);
        }
Example #9
0
        /// <summary>
        /// Draws the control in normal or pushed states
        /// </summary>
        /// <param name="e">Paitn event</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            var attributes = new ImageAttributes();

            using (var g = Graphics.FromImage(MemoryBitmap))
            {
                if (Pushed)
                {
                    using (var pen = new Pen(Theme.ThemeBase))
                        GraphicsExtensions.DrawThemedGradientRectangle(g, pen, ClientRectangle, Dpi.ScaleSize(new Size(8, 8)));
                }
                else
                {
                    g.Clear(Parent.BackColor);
                }

                var textSize = g.MeasureString(Text, Font);
                var textArea = new RectangleF((ClientSize.Width - textSize.Width) / 2,
                                              (ClientSize.Height - textSize.Height),
                                              textSize.Width,
                                              textSize.Height);

                if (Image != null)
                {
                    var imageWidth  = Dpi.Scale(Image.Width);
                    var imageHeight = Dpi.Scale(Image.Height);
                    var imageArea   = new Rectangle((ClientSize.Width - imageWidth) / 2,
                                                    (ClientSize.Height - imageHeight) / 2,
                                                    imageWidth,
                                                    imageHeight);

                    var key = Image.GetPixel(0, 0);
                    attributes.SetColorKey(key, key);

                    g.DrawImage(Image,
                                StretchImage ? ClientRectangle : imageArea,
                                0, 0, Image.Width, Image.Height,
                                GraphicsUnit.Pixel,
                                attributes);
                }

                using (var brush = new SolidBrush(ForeColor))
                    g.DrawString(Text, Font, brush, textArea);

                if (Pushed)
                {
                    var key = MemoryBitmap.GetPixel(0, 0);
                    attributes.SetColorKey(key, key);
                }
                else
                {
                    attributes.ClearColorKey();
                }

                e.Graphics.DrawImage(MemoryBitmap,
                                     ClientRectangle,
                                     0, 0, MemoryBitmap.Width, MemoryBitmap.Height,
                                     GraphicsUnit.Pixel,
                                     attributes);
            }
        }
Example #10
0
        private void ClearColorKey_Click(object sender, System.EventArgs e)
        {
            ImageAttributes ImgAttr = new ImageAttributes();

            ImgAttr.ClearColorKey(ColorAdjustType.Default);
        }
Example #11
0
 private void DrawForegroundFromButton(PaintEventArgs pevent)
 {
     if (_imageButton == null)
     {
         _imageButton        = new Button();
         _imageButton.Parent = new TransparentControl();
         _imageButton.SuspendLayout();
         _imageButton.BackColor = Color.Transparent;
         _imageButton.FlatAppearance.BorderSize = 0;
         _imageButton.FlatStyle = FlatStyle.Flat;
     }
     else
     {
         _imageButton.SuspendLayout();
     }
     _imageButton.AutoEllipsis = AutoEllipsis;
     if (Enabled)
     {
         _imageButton.ForeColor = ForeColor;
     }
     else
     {
         _imageButton.ForeColor = Color.FromArgb((3 * ForeColor.R + _backColor.R) >> 2,
                                                 (3 * ForeColor.G + _backColor.G) >> 2,
                                                 (3 * ForeColor.B + _backColor.B) >> 2);
     }
     _imageButton.Font        = Font;
     _imageButton.RightToLeft = RightToLeft;
     _imageButton.Image       = Image;
     if (Image != null && !Enabled)
     {
         Size size           = Image.Size;
         var  newColorMatrix = new float[5][];
         newColorMatrix[0] = new[] { 0.2125f, 0.2125f, 0.2125f, 0f, 0f };
         newColorMatrix[1] = new[] { 0.2577f, 0.2577f, 0.2577f, 0f, 0f };
         newColorMatrix[2] = new[] { 0.0361f, 0.0361f, 0.0361f, 0f, 0f };
         var arr = new float[5];
         arr[3]            = 1f;
         newColorMatrix[3] = arr;
         newColorMatrix[4] = new[] { 0.38f, 0.38f, 0.38f, 0f, 1f };
         var matrix            = new ColorMatrix(newColorMatrix);
         var disabledImageAttr = new ImageAttributes();
         disabledImageAttr.ClearColorKey();
         disabledImageAttr.SetColorMatrix(matrix);
         _imageButton.Image = new Bitmap(Image.Width, Image.Height);
         using (Graphics gr = Graphics.FromImage(_imageButton.Image))
         {
             gr.DrawImage(Image, new Rectangle(0, 0, size.Width, size.Height), 0, 0, size.Width, size.Height,
                          GraphicsUnit.Pixel, disabledImageAttr);
         }
     }
     _imageButton.ImageAlign                 = ImageAlign;
     _imageButton.ImageIndex                 = ImageIndex;
     _imageButton.ImageKey                   = ImageKey;
     _imageButton.ImageList                  = ImageList;
     _imageButton.Padding                    = Padding;
     _imageButton.Size                       = Size;
     _imageButton.Text                       = Text;
     _imageButton.TextAlign                  = TextAlign;
     _imageButton.TextImageRelation          = TextImageRelation;
     _imageButton.UseCompatibleTextRendering = UseCompatibleTextRendering;
     _imageButton.UseMnemonic                = UseMnemonic;
     _imageButton.ResumeLayout();
     InvokePaint(_imageButton, pevent);
     if (_imageButton.Image != null && _imageButton.Image != Image)
     {
         _imageButton.Image.Dispose();
         _imageButton.Image = null;
     }
 }