如何在运行时更改Image的颜色

时间:2022-01-23 09:33:11

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.

我想知道有什么方法可以在运行时更改图像颜色。例如,假设我有一个JPG绑定到ASP.Net的Image控件。接下来我有一个下拉列表,它给了我各种颜色选项,如红色,格力等。我现在想将图像的颜色更改为droprdown列表中选择的颜色。

3 个解决方案

#1


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:

下面是一个加载JPEG的代码示例,将图像中的任何红色像素更改为蓝色,然后在图片框中显示位图:

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.

警告:GetPixel和SetPixel非常慢。如果您的图像很大和/或性能是一个问题,那么在.NET中读取和写入像素的方法要快得多,但这需要更多的工作。

#2


You also try this for web (asp.net) , you can ignore the logic but can see what getpixel & setpixel doing

您也可以尝试使用web(asp.net),您可以忽略逻辑,但可以看到getpixel和setpixel正在做什么

 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);
  }

#3


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

用于在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); 
}

#1


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:

下面是一个加载JPEG的代码示例,将图像中的任何红色像素更改为蓝色,然后在图片框中显示位图:

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.

警告:GetPixel和SetPixel非常慢。如果您的图像很大和/或性能是一个问题,那么在.NET中读取和写入像素的方法要快得多,但这需要更多的工作。

#2


You also try this for web (asp.net) , you can ignore the logic but can see what getpixel & setpixel doing

您也可以尝试使用web(asp.net),您可以忽略逻辑,但可以看到getpixel和setpixel正在做什么

 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);
  }

#3


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

用于在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); 
}