?
Solved

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

Posted on 2011-10-18
3
Medium Priority
?
1,193 Views
Last Modified: 2012-05-12
Experts,

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();
    attributes.SetRemapTable(map);

    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?
0
Comment
Question by:P1ST0LPETE
  • 2
3 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 36987510
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.
0
 
LVL 10

Accepted Solution

by:
P1ST0LPETE earned 0 total points
ID: 37028846
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.
0
 
LVL 10

Author Closing Comment

by:P1ST0LPETE
ID: 37052396
Ended up figuring out the solution on my own.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

864 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