【发布时间】:2016-08-17 17:10:10
【问题描述】:
我试图在 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;
}
谢谢!
【问题讨论】:
-
使用BlurEffect。不要试图重新发明轮子。
-
我相信你的img对象只有在这个方法完成后才存在于内存中......你不必将它写到一个新文件中吗?
-
@HighCore
BlureEffect只能在DependencyObject上使用,并非在所有情况下都可用。 -
@AmirOveisi 在什么情况下您的
Visual不是DependencyObject? -
@HighCore 如果你不使用
WPF/SL项目,那么你就没有DependencyObject!这里@user2816235 使用带有EventArgs的代码来单击按钮而不是RoutedEventArgs。所以我认为这不是WPF/SL相关问题,而是关于WinFrom。所以这里没有DependencyObject。