C# 如何在运行时更改图像的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/383103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:23:08  来源:igfitidea点击:

How to change color of Image at runtime

c#asp.netimage

提问by

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 的图像控件。接下来,我有一个下拉列表,它为我提供了各种颜色选项,如红色、绿色等。我现在想将图像的颜色更改为下拉列表中选择的颜色。

回答by MusiGenesis

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 中读取和写入像素的速度要快得多,但需要做更多的工作。

回答by MaRaM

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

回答by Mohit Verma

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