.NET GDI+ Icons Windows Forms - Draw Selected Color On Icon

By Robbe Morris

Learn to draw a selected color bar on an icon for font color, fill color, and line color. Office icons with the color bar stripped are included in this code sample.

  Download Source Code

Here is a quick screen shot of the sample code demonstrating the selected color
as being red.



Here is the source code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EggHeadCafe.Drawing;

namespace EggHeadCafe.IconDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void cmdFontColor_Click(object sender, EventArgs e)
        {
            System.Drawing.Color color = GetColorFromDialog();
            IconController.DrawColorBar(this.tsbFontColor.Image,color); 
            this.tsbFontColor.Invalidate();  
        }

        private void cmdLineColor_Click(object sender, EventArgs e)
        {
            System.Drawing.Color color = GetColorFromDialog();
            IconController.DrawColorBar(this.tsbLineColor.Image,color); 
            this.tsbLineColor.Invalidate(); 
        }

        private void cmdFillColor_Click(object sender, EventArgs e)
        {
            System.Drawing.Color color = GetColorFromDialog();
            IconController.DrawColorBar(this.tsbFillColor.Image,color);  
            this.tsbFillColor.Invalidate();
        }

        private System.Drawing.Color GetColorFromDialog()
        {
            if (this.colorDialog1.ShowDialog() == DialogResult.OK)
            {
                return this.colorDialog1.Color;
            }
            return Color.Empty;
        }

    }
}




using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D; 

namespace EggHeadCafe.Drawing
{
    public class IconController
    {
        #region Draw Color Bar
        public static void DrawColorBar(Image icon,Color color,int x,int y, int width)
         {
               int height = 2;
             
               try
               {
                     if (color == Color.Empty) { return; }

                     Graphics g = Graphics.FromImage(icon);

                     g.SmoothingMode = SmoothingMode.AntiAlias;

                     Rectangle bgImg = new Rectangle(x, y, width, height);
 
                     SolidBrush bgBrush = new SolidBrush(color);

                     g.FillRectangle(bgBrush, bgImg);

                     bgBrush.Dispose();
              
               }
             catch (Exception) { throw; }
           }
        #endregion

        #region Draw Color Bar
        public static void DrawColorBar(Image icon,Color color,int x, int y)
        {

            try
            {
                DrawColorBar(icon,color,x,y,14);
            }
            catch (Exception) { throw; }
        }
        #endregion

        #region Draw Color Bar
        public static void DrawColorBar(Image icon,Color color)
        {

            try
            {
                DrawColorBar(icon,color,1,13);
            }
            catch (Exception) { throw; }
        }
        #endregion

       }
}
Popularity  (991 Views)
Picture
Biography - Robbe Morris
Robbe has been a Microsoft MVP in C# since 2004. He is also the co-founder of EggHeadCafe.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.  Robbe also loves to scuba dive and go deep sea fishing in the Florida Keys or off the coast of Daytona Beach. Microsoft MVP
Create New Account
Article Discussion: .NET Icons Windows Forms - Draw Selected Color On Icon
Robbe Morris posted at Tuesday, August 14, 2007 8:03 PM