Changing a specific color in an Image in C#.Net

Posted on 2011-10-18
Last Modified: 2012-05-12

I'm working on an Asp.Net MVC project, and one of my requirements is to dynamically change the color of the icons on the page.  An example icon is shown here:
 Sample Calendar Image
I have been researching this the last few days, and feel that I am pretty close, however I have little graphic programming experience and need some help figuring out where I'm going wrong.  Here is currently what I'm working with:

I have images on the page that look like this:
<img src="/Organization/GetIcon/calendar" />

And then I have a controller Action Result that basically looks like this:
public ActionResult GetIcon(string name)
    //Temporarily disregarding name and just loading up test image:
    Image image = Image.FromFile(Server.MapPath("/Content/images/calendar.gif"));

    Bitmap bmp = new Bitmap(image);

    ColorMap[] map = new ColorMap[1];
    map[0] = new ColorMap();
    map[0].OldColor = Color.FromArgb(46, 177, 53);  //The green in the calendar image
    map[0].NewColor = Color.FromArgb(255, 0, 0);    //Red

    ImageAttributes attributes = new ImageAttributes();

    Graphics graphic = Graphics.FromImage(bmp);
    Rectangle rectangle = new Rectangle(0, 0, bmp.Width, bmp.Height);
    graphic.DrawImage(bmp, rectangle, 0, 0, rectangle.Width, rectangle.Height, graphic.PageUnit, attributes);

    Byte[] imageBytes = null;

    //Convert newly colored image to byte array.....

    return File(imageBytes, "image/gif");

Open in new window

So my questions:
 - Is the way I am attempting to change the color a correct approach?
 - How do I get the newly colored image to a byte[] array?
Question by:P1ST0LPETE
    LVL 28

    Expert Comment

    What I would recommend is a much simpler solution... create the graphic you need with the part to change color transparent. Then place a background color to the image and it will show through the transparent portions.
    LVL 10

    Accepted Solution

    Using a transparent image sitting on top of a colored background was my first thought too.  However, it became too much trouble, especially when designing it for multiple browser support and using jquery plugins that were looking to be passed just an image.  In the end it was easier to just change the color of the actual image itself.  Also, I ended up having to use .png image files, because .gif and .jpg were not able to save the true color of the image in a way that was easily changable by code.
    LVL 10

    Author Closing Comment

    Ended up figuring out the solution on my own.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now