search
Japanese Chinese Nederlands Espanol Italiano Deutsch Francais Twitter Rss Feeds
MicrosoftArticlesForumsFAQs
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml / Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

Web ProgrammingArticlesForumsFAQs
JavaScript
ASP
ASP.NET
Web Services

Non-MicrosoftArticlesForumsFAQs
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

DatabasesArticlesForumsFAQs
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsFAQs
Excel
Word
Powerpoint
Outlook
Publisher
Money

Operating SystemsArticlesForumsFAQs
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsFAQs
BizTalk
Site Server
Exhange Server
IIS

Graphic DesignArticlesForumsFAQs
Macromedia Flash
Adobe PhotoShop
Expression Blend
Expression Design
Expression Web

OtherArticlesForumsFAQs
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Product Reviews
Search Engines
Resumes

 

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


By Robbe Morris
Printer Friendly Version
View My Articles
22 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.

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