logo

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

By Robbe Morris
Printer Friendly Version
View My Articles
146 Views
    

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 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.


Didn't Find The Answer You Were Looking For?

EggHeadCafe has experts online right now that may know the answer to your question.  We pay them a bonus for answering as many questions as they can.  So, why not help them and yourself by becoming a member (free) and ask them your question right now?
Ask Question In Live Forum

If you have an OpenID and do not want to become a member of the EggHeadCafe forum, you can also sign on to Chat Chaos and post your question to our real time Silverlight chat application.
Ask Question In Chat Chaos

Article Discussion: .NET Icons Windows Forms - Draw Selected Color On Icon
Robbe Morris posted at Tuesday, August 14, 2007 8:03 PM
Original Article
 






  $1000 Contest    [)ia6l0 iii - $231  |  Jonathan VH - $153  |  Huggy Bear - $133  |  egg egg - $100  |  F Cali - $93  |  more Advertise  |  Privacy  |   (c) 2010