c# - 在 C# 中更快地反转图像

标签 c# .net colors picturebox

我正在使用 WinForms。我的表格中有一个图片框。当我在图片框中打开图片时,我可以通过单击按钮来回反转颜色,但我的代码非常慢。我怎样才能提高性能。

   private void Button1_Click(object sender, System.EventArgs e) 
     {
        Bitmap pic = new Bitmap(PictureBox1.Image);
        for (int y = 0; (y 
                    <= (pic.Height - 1)); y++) {
            for (int x = 0; (x 
                        <= (pic.Width - 1)); x++) {
                Color inv = pic.GetPixel(x, y);
                inv = Color.FromArgb(255, (255 - inv.R), (255 - inv.G), (255 - inv.B));
                pic.SetPixel(x, y, inv);
                PictureBox1.Image = pic;
            }

        }

    }

最佳答案

每次更改像素时都会设置控件的图片,这会导致控件重绘自身。等到你完成图像:

Bitmap pic = new Bitmap(PictureBox1.Image);
for (int y = 0; (y <= (pic.Height - 1)); y++) {
    for (int x = 0; (x <= (pic.Width - 1)); x++) {
        Color inv = pic.GetPixel(x, y);
        inv = Color.FromArgb(255, (255 - inv.R), (255 - inv.G), (255 - inv.B));
        pic.SetPixel(x, y, inv);
    }
}
PictureBox1.Image = pic;

关于c# - 在 C# 中更快地反转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33024881/

相关文章:

c# - 如何以编程方式诊断原因、修复或解决与 Adob​​e ActiveX/COM 相关的错误 0x80004005?

flutter - 如何在 Flutter 上生成具有不同随机起点的随机模糊渐变颜色背景?

c# - Cake-Experimental Switch 编译错误

c# - 如何在 xamarin 中添加 System.Drawing?

.net - 在 Azure 上生成失败 - 找不到 .NETFramework,Version=v4.7.2 的引用程序集

.net - LINQ to SQL 左外连接

html - 阴影标题显示不正确

java - Android:使用 getResources 可能会导致我的应用程序崩溃

c# - ASP.NET MVC Razor 获取文本框值

c# - 使用索引器从数据存储中检索 Linq to SQL 对象