【问题标题】:Invert image faster in C#在 C# 中更快地反转图像
【发布时间】:2016-01-06 14:41:41
【问题描述】:

我正在使用 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;
            }

        }

    }

【问题讨论】:

    标签: c# .net colors picturebox


    【解决方案1】:

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

    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;
    

    【讨论】:

    • @D Stanley 感谢您的解释和举例。它就像一个魅力。
    • Get/SetPixel 非常慢。 Google for LockBits 并使用它 - 您可以更快地获得结果。
    • @LucasTrzesniewski 我会调查的。不过,在这种情况下,我对如何使用 LockBits 不是很熟悉。如果您能举个例子,我将不胜感激。
    • @taji01 这个网站上有无数关于这个话题的问题,只要搜索一下,你会发现比你期望的更多;)
    • 我找到了一对,但我不知道如何将它合并到按钮点击中,就像我拥有它一样。 @LucasTrzesniewski
    【解决方案2】:

    如果有人需要 VB.NET 中的类似代码。

    还要注意变量 inv.A 而不是值 255。如果你的图片框有透明度,你需要这个。

        Public Function InvertImageColors(ByVal p As Image) As Image
            Dim pic As New Bitmap(p)
            For y As Integer = 0 To pic.Height - 1
                For x As Integer = 0 To pic.Width - 1
                    Dim inv As Color = pic.GetPixel(x, y)
                    inv = Color.FromArgb(inv.A, 255 - inv.R, 255 - inv.G, 255 - inv.B)
                    pic.SetPixel(x, y, inv)
                Next x
            Next y
            Return pic
        End Function
    

    用法:

        pic.image = InvertImageColors(pic.image)
    

    【讨论】:

      【解决方案3】:

      这对我有用...

              private Image InvertingImage(Image source)
          {
              //create a blank bitmap the same size as original
              Bitmap newBitmap = new Bitmap(source.Width, source.Height);
      
              //get a graphics object from the new image
              Graphics g = Graphics.FromImage(newBitmap);
      
              // create the negative color matrix
              ColorMatrix colorMatrix = new ColorMatrix(new float[][]
      {
          new float[] {-1, 0, 0, 0, 0},
          new float[] {0, -1, 0, 0, 0},
          new float[] {0, 0, -1, 0, 0},
          new float[] {0, 0, 0, 1, 0},
          new float[] {1, 1, 1, 0, 1}
      });
      
              // create some image attributes
              ImageAttributes attributes = new ImageAttributes();
      
              attributes.SetColorMatrix(colorMatrix);
      
              g.DrawImage(source, new Rectangle(0, 0, source.Width, source.Height),
                          0, 0, source.Width, source.Height, GraphicsUnit.Pixel, attributes);
      
              //dispose the Graphics object
              g.Dispose();
      
              return newBitmap;
          }
      

      【讨论】:

      • 此代码似乎与this answer(包括 cmets)中的代码相同,后者本身是从here 复制的(注明出处)。如果是从别处复制的,应注明原件,避免plagiarism。
      猜你喜欢
      • 2012-05-06
      • 2010-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-03
      • 1970-01-01
      • 2010-12-19
      • 1970-01-01
      相关资源
      最近更新 更多