反转图像更快地在C#更快、图像

2023-09-06 10:14:00 作者:一身仙女味

我使用的WinForms。我有一个图片框在我的形式。当我打开图片框的图片我能够反转的颜色来回上的一个按钮的点击,但我的code是极其缓慢。我怎样才能提高性能。

 私人无效的button1_Click(对象发件人,发送System.EventArgs)
     {
        位图PIC =新位图(PictureBox1.Image);
        对于(INT Y = 0;(Y
                    &其中; =(pic.Height  -  1)); Ÿ++){
            为(中间体X = 0;(X
                        &其中; =(pic.Width  -  1)); X ++){
                色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 =新位图(PictureBox1.Image);
为(中间体y = 0的;(γ&其中; =(pic.Height  -  1)); Y ++){
    为(中间体X = 0;(X&其中; =(pic.Width  -  1)); X ++){
        色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 =知情同意;
 
一套永远赚大钱的股票抄底系统,读懂此文,亏钱的一定不是你

I'm using WinForms. I have a picture-box in my form. When I open a picture in the picture-box I am able to invert the colors back and forth on a click of a button, but my code is extremely slow. How can I increase the performance.

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

        }

    }

解决方案

You are setting the control's picture each time you change a pixel, which causes the control to redraw itself. Wait until you've finished the image:

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;