[ACCEPTED]-How to change color of Image at runtime-image

Accepted answer
Score: 12

Here is a code sample that loads a JPEG, changes 7 any red pixels in the image to blue, and 6 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 5 and SetPixel are incredibly slow. If your 4 images are large and/or performance is an 3 issue, there is a much faster way to read 2 and write pixels in .NET, but it's a little 1 bit more work.

Score: 0

You also try this for web (asp.net) , you 2 can ignore the logic but can see what getpixel 1 & 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);
  }

More Related questions