Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将图像转换为灰度C#Visual 14_C#_Image_Bitmap_Picturebox - Fatal编程技术网

将图像转换为灰度C#Visual 14

将图像转换为灰度C#Visual 14,c#,image,bitmap,picturebox,C#,Image,Bitmap,Picturebox,我在通过Microsoft visual将图像转换为C#中的灰度时遇到了一些问题 目前,我已经在GUI中设置了代码,可以调整图像大小, 我希望能够通过单击按钮将显示的图像转换为灰度。代码在下面!。一旦我按下灰度按钮,我的应用程序就会冻结。我错在哪里 private void buttonGrayscale_Scale(object sender, EventArgs e) { Bitmap bmMyImage = new Bitmap((Bitmap)Picture

我在通过Microsoft visual将图像转换为C#中的灰度时遇到了一些问题

目前,我已经在GUI中设置了代码,可以调整图像大小, 我希望能够通过单击按钮将显示的图像转换为灰度。代码在下面!。一旦我按下灰度按钮,我的应用程序就会冻结。我错在哪里

   private void buttonGrayscale_Scale(object sender, EventArgs e)
    {
        Bitmap bmMyImage = new Bitmap((Bitmap)PictureBox1.Image);
        bmMyImage=MakeGrayscale(bmMyImage);

        PictureBox1.Image = (Image)bmMyImage;
    }

    public static Bitmap MakeGrayscale(Bitmap original)
    {
        //make an empty bitmap the same size as orgininal
        Bitmap newBitmap = new Bitmap(original.Width, original.Height);

        for (int i = 0; i < original.Width; i++)
        {
            for (int j = 0; j < original.Height; j++)
            {
                Color c = newBitmap.GetPixel(i, j);

                int r = c.R;
                int g = c.G;
                int b = c.B;
                int avg = (r + g + b) / 3;
                newBitmap.SetPixel(i, j, Color.FromArgb(avg, avg, avg));

            }
        }

        return newBitmap;
    }
private void按钮rayscale\u Scale(对象发送方,事件参数e)
{
位图bmMyImage=新位图((位图)PictureBox1.Image);
bmMyImage=MakeGrayscale(bmMyImage);
PictureBox1.Image=(Image)bmMyImage;
}
公共静态位图生成灰度(位图原始)
{
//使空位图的大小与orgininal相同
位图newBitmap=新位图(original.Width、original.Height);
对于(int i=0;i
也许您想从原始位图中获取像素,而不是从空位图中获取像素

Color c = original.GetPixel(i, j);
您不需要将位图转换为图像

private void buttonGrayscale_Scale(object sender, EventArgs e)
    {
        Bitmap bmMyImage=MakeGrayscale(PictureBox1.Image);

        PictureBox1.Image=bmMyImage;
    }
其功能是:

public static Bitmap MakeGrayscale(Image original)
...

尝试设置一些类似于进度指示的内容,然后您可能会看到答案;这一行代码很有帮助。非常感谢。