I would like to know is there any way by which we can change the Image color at runtime. for e.g lets say I am having a JPG bind to an Image control of ASP.Net. Next I am having a dropdown List which gives me the various color option like red,gree,etc. I would now like to change the color of the Image to the one selected in the droprdown List.
Asked
Active
Viewed 3.2k times
3
-
You'll need to say more about what you're doing. "The image" doesn't mean anything to anyone else. – Will Dean Dec 20 '08 at 08:56
-
Do you have a picture you want to manipulate, or is it because you need illustrate the selected color? What im trying to figure out is, if you want to make a picture of, lets say a dog, more greener when green is selected. Or you want to just show a picture of a green color. – Tom Jelen Dec 20 '08 at 13:25
-
It sounds to me like the OP wants to change the color of an "image" client-side. Could you better define what "image" means in this context? Do you mean the JPG, a border/background to the JPG? – Russ Cam Jun 02 '09 at 11:16
3 Answers
12
Here is a code sample that loads a JPEG, changes any red pixels in the image to blue, and then displays the bitmap in a picture box:
Bitmap bmp = (Bitmap)Bitmap.FromFile("image.jpg");
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
if (bmp.GetPixel(x, y) == Color.Red)
{
bmp.SetPixel(x, y, Color.Blue);
}
}
}
pictureBox1.Image = bmp;
Warning: GetPixel and SetPixel are incredibly slow. If your images are large and/or performance is an issue, there is a much faster way to read and write pixels in .NET, but it's a little bit more work.

MusiGenesis
- 74,184
- 40
- 190
- 334
-
1Comparing the GetPixel result with a Color doesn't always work. You should compare their ToArbg() results as shown here: http://stackoverflow.com/questions/7464994/how-to-compare-a-color-by-the-getpixel-method-and-a-color-passed-in-a-method-lik – FoppyOmega Feb 06 '13 at 18:08
-
Actually, I'm not sure my code would *ever* work. I think even if the bitmap's pixel color has the exact same ARGB values as Color.Red, it still will not be equivalent to Color.Red. – MusiGenesis Feb 06 '13 at 19:01
0
You also try this for web (asp.net) , you can ignore the logic but can see what getpixel & setpixel doing
public string FileUpload( HttpPostedFileBase file )
{
Bitmap bmp = new Bitmap(file.InputStream);
string valid = "";
for(int i = 0; i < bmp.Width; i++) {
for(int j = 0; j < bmp.Height; j++) {
if(bmp.GetPixel(i , j).B < 20) {
if(bmp.GetPixel(i , j).B == bmp.GetPixel(i , j).G &&
bmp.GetPixel(i , j).B == bmp.GetPixel(i , j).R) {
valid = valid + bmp.GetPixel(i , j). + "<br/>";
bmp.SetPixel(i , j , Color.DarkGreen);
}
}
}
}
SaveImage(bmp);
return valid;
}
private void SaveImage( Bitmap newbmp )
{
string path = Path.Combine(Server.MapPath("~/Images") , "ScaledImage.jpeg");
newbmp.Save(path , System.Drawing.Imaging.ImageFormat.Jpeg);
}

Mohit Verma
- 1,620
- 2
- 20
- 30
-1
I am also facing trouble to under this question. after that based on the some information. I wrote the code manually.Now it works well. If you want to check.you can use it.
code for change the background image during runtime in C#.net
you can use simply this code. That is, ==>
string str;
OpenFileDialog od = new OpenFileDialog();
if (od.ShowDialog() == DialogResult.OK)
{
str = od.FileName;
this.BackgroundImage=Image.FromFile(str);
}