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


By Robbe Morris
Printer Friendly Version
  

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

       }
}


Biography
Robbe is a 2004-2008 Microsoft MVP for C# and the .NET Evangelist for Alinean Inc..  He is also the co-founder of EggHeadCafe. Robbe enjoys scuba diving with the folks at wet-n-fla.


button
 
Article Discussion: .NET Icons Windows Forms - Draw Selected Color On Icon
  Robbe Morris posted at 14-Aug-07 08:03
Original Article