我试图通过遍历一个图像的所有像素来模糊 C# 中的图像,然后创建一个新的位图,其中原始图像中像素的颜色除以像素数以创建平均颜色。当我运行它时,什么也没有发生。这是代码:
private void blurToolStripMenuItem_Click(object sender, EventArgs e)
{
Bitmap img = new Bitmap(pictureBox1.Image);
Bitmap blurPic = new Bitmap(img.Width, img.Height);
Int32 avgR = 0, avgG = 0, avgB = 0;
Int32 blurPixelCount = 0;
for (int y = 0; y < img.Height; y++)
{
for (int x = 0; x < img.Width; x++)
{
Color pixel = img.GetPixel(x, y);
avgR += pixel.R;
avgG += pixel.G;
avgB += pixel.B;
blurPixelCount++;
}
}
avgR = avgR / blurPixelCount;
avgG = avgG / blurPixelCount;
avgB = avgB / blurPixelCount;
for (int y = 0; y < img.Height; y++)
{
for (int x = 0; x < img.Width; x++)
{
blurPic.SetPixel(x, y, Color.FromArgb(avgR, avgG, avgB));
}
}
img = blurPic;
}
谢谢!