如何在 C# 中对位图图像应用模糊效果? [关闭]

2023-11-21

如何在不使用库的情况下在 C# 中对图像应用模糊效果?


更新的代码(现在更快,需要使用 UNSAFE 关键字)

static void Main(string[] args)
{
    Bitmap bitmap = new Bitmap("C:\\Users\\erik\\test.png");
    bitmap = Blur(bitmap, 10);
    bitmap.Save("C:\\Users\\erik\\test2.png");
}

private static Bitmap Blur(Bitmap image, Int32 blurSize)
{
    return Blur(image, new Rectangle(0, 0, image.Width, image.Height), blurSize);
}

private unsafe static Bitmap Blur(Bitmap image, Rectangle rectangle, Int32 blurSize)
{
    Bitmap blurred = new Bitmap(image.Width, image.Height);

    // make an exact copy of the bitmap provided
    using (Graphics graphics = Graphics.FromImage(blurred))
        graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height),
            new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);

    // Lock the bitmap's bits
    BitmapData blurredData = blurred.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, blurred.PixelFormat);

    // Get bits per pixel for current PixelFormat
    int bitsPerPixel = Image.GetPixelFormatSize(blurred.PixelFormat);

    // Get pointer to first line
    byte* scan0 = (byte*)blurredData.Scan0.ToPointer();

    // look at every pixel in the blur rectangle
    for (int xx = rectangle.X; xx < rectangle.X + rectangle.Width; xx++)
    {
        for (int yy = rectangle.Y; yy < rectangle.Y + rectangle.Height; yy++)
        {
            int avgR = 0, avgG = 0, avgB = 0;
            int blurPixelCount = 0;

            // average the color of the red, green and blue for each pixel in the
            // blur size while making sure you don't go outside the image bounds
            for (int x = xx; (x < xx + blurSize && x < image.Width); x++)
            {
                for (int y = yy; (y < yy + blurSize && y < image.Height); y++)
                {
                    // Get pointer to RGB
                    byte* data = scan0 + y * blurredData.Stride + x * bitsPerPixel / 8;

                    avgB += data[0]; // Blue
                    avgG += data[1]; // Green
                    avgR += data[2]; // Red

                    blurPixelCount++;
                }
            }

            avgR = avgR / blurPixelCount;
            avgG = avgG / blurPixelCount;
            avgB = avgB / blurPixelCount;

            // now that we know the average for the blur size, set each pixel to that color
            for (int x = xx; x < xx + blurSize && x < image.Width && x < rectangle.Width; x++)
            {
                for (int y = yy; y < yy + blurSize && y < image.Height && y < rectangle.Height; y++)
                {
                    // Get pointer to RGB
                    byte* data = scan0 + y * blurredData.Stride + x * bitsPerPixel / 8;

                    // Change values
                    data[0] = (byte)avgB;
                    data[1] = (byte)avgG;
                    data[2] = (byte)avgR;
                }
            }
        }
    }

    // Unlock the bits
    blurred.UnlockBits(blurredData);

    return blurred;
}

Took 2.356 seconds处理256x256具有模糊值的图像10.

原始代码(来自Github- 略有改动)

static void Main(string[] args)
{
    Bitmap bitmap = new Bitmap("C:\\Users\\erik\\test.png");

    bitmap = Blur(bitmap, 10);

    bitmap.Save("C:\\Users\\erik\\test2.png");
}

private static Bitmap Blur(Bitmap image, Int32 blurSize)
{
    return Blur(image, new Rectangle(0, 0, image.Width, image.Height), blurSize);
}

private static Bitmap Blur(Bitmap image, Rectangle rectangle, Int32 blurSize)
{
    Bitmap blurred = new Bitmap(image.Width, image.Height);

    // make an exact copy of the bitmap provided
    using (Graphics graphics = Graphics.FromImage(blurred))
        graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height),
            new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);

    // look at every pixel in the blur rectangle
    for (int xx = rectangle.X; xx < rectangle.X + rectangle.Width; xx++)
    {
        for (int yy = rectangle.Y; yy < rectangle.Y + rectangle.Height; yy++)
        {
            int avgR = 0, avgG = 0, avgB = 0;
            int blurPixelCount = 0;

            // average the color of the red, green and blue for each pixel in the
            // blur size while making sure you don't go outside the image bounds
            for (int x = xx; (x < xx + blurSize && x < image.Width); x++)
            {
                for (int y = yy; (y < yy + blurSize && y < image.Height); y++)
                {
                    Color pixel = blurred.GetPixel(x, y);

                    avgR += pixel.R;
                    avgG += pixel.G;
                    avgB += pixel.B;

                    blurPixelCount++;
                }
            }

            avgR = avgR / blurPixelCount;
            avgG = avgG / blurPixelCount;
            avgB = avgB / blurPixelCount;

            // now that we know the average for the blur size, set each pixel to that color
            for (int x = xx; x < xx + blurSize && x < image.Width && x < rectangle.Width; x++)
                for (int y = yy; y < yy + blurSize && y < image.Height && y < rectangle.Height; y++)
                    blurred.SetPixel(x, y, Color.FromArgb(avgR, avgG, avgB));
        }
    }

    return blurred;
}

Took 7.594 seconds处理256x256具有模糊值的图像10.

原始图像

Original Image

图像模糊(模糊级别 10)

Blurred Image

图片来自:https://www.pexels.com/search/landscape/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 C# 中对位图图像应用模糊效果? [关闭] 的相关文章

随机推荐

  • LabelEncoder 将缺失值保留为“NaN”

    我正在尝试使用标签编码器将分类数据转换为数值 我需要一个 LabelEncoder 将缺失值保留为 NaN 以便之后使用 Imputer 所以我想在像这样标记后使用掩码来替换原始数据框 df pd DataFrame A x np NaN
  • NSOpenPanel 作为工作表

    我环顾四周的其他答案 但似乎没有什么可以帮助我的情况 我有一个 viewController 类 其中包含按钮的 IBAction 此按钮应该从该 viewController 打开一个 NSOpenPanel 作为工作表 class Vi
  • Angular 在父组件嵌套表单中使用子组件表单

    将子组件表单放入父组件表单的最佳方法是什么 我们使用的是2019年最新的Angular 8 经过研究 以下方法并不完全有效 父组件 ngOnInit this parentForm this fb group childForm1 etc
  • 我可以在过程中传递游标吗?

    我可以在过程中传递游标吗 CURSOR BLT CURSOR IS SELECT BLT sol id BLT bill id BLT bank id FROM BLT 是我的光标 Procedure abc i want to pass
  • 括号组合的时间复杂度

    我尝试做经典问题来实现一种算法来打印 n 对括号的所有有效组合 我找到了这个程序 效果很好 public static void addParen ArrayList
  • 您可以在网页上将图片相互叠加吗?

    我想建立一个 装扮 游戏网站 您可以单击不同的配件 它们将彼此叠加 因为描述起来有点困难 我找到了这些例子 希望能够突出我正在尝试做的事情 这是我找到的最接近的网站 但我似乎找不到计算图像应如何彼此重叠的代码 类似于这个 iOS 公主游戏
  • 在 Maven 中测试 Clojure

    我是 Maven 的新手 甚至是 Clojure 的新手 作为学习语言的练习 我正在编写一个蜘蛛纸牌播放器程序 我还计划在 Scala 中编写一个类似的程序来比较实现 请参阅我的帖子https stackoverflow com quest
  • ggplot - 按函数输出分面

    我不确定如何通过数据中的函数进行分面data的元素ggplot目的 在下面的玩具示例中 我想做的是这样的 df lt data frame x 1 8 y runif 8 z 8 1 ggplot df aes x x y y geom p
  • 使用 SQL Server XML 数据类型

    我有一个包含 XML 字段的表 它包含的典型 XML 是
  • 如何将 Nuget 包 dll 添加到 Wix 安装程序

    如何将 Nuget 包 dll 添加到 Wix 安装程序 我尝试添加
  • Android:从电话号码中检索联系人姓名

    我想检索与来电号码关联的联系人姓名 当我处理广播接收器中的传入号码时 具有带有来电呼叫者姓名的字符串将极大地帮助我的项目 我认为这涉及使用 sql WHERE 子句作为过滤器的查询 但我需要对联系人进行排序吗 一个例子或提示会有很大的帮助
  • 如何在android中的其他视图上添加覆盖视图?

    我想将按钮放在图像视图上方 我怎样才能做到这一点 请不要提供设置背景 因为我需要 ImageView 设置为背景 开玩笑 你需要的是把你的观点放在一个RelativeLayout 类似的东西会起作用
  • Python Excel 模板读取和重写,维护公式和格式

    我已经查遍了所有内容 但似乎找不到我要找的东西 我在这里找到的所有线索对我来说最终都陷入了死胡同 xlrd xlwt 和 xlutils 几乎可以满足我的需要 但是 基本思想是我需要使用 Python 将简单数据 字符串 写入 Excel
  • 如何在 QML 中集成 QWidget(Qt Quick 2.0)

    我已经关闭了返回 QFrame 的库 我的程序的GUI是用QML Qt Quick 2 0 开发的 我需要将 QFrame QWidget 集成到 QML 的解决方案 笔记 我找到了示例 Qt DIR Examples Qt 5 3 dec
  • Java StringTokenizer,空空标记

    我正在尝试将一个字符串拆分为 29 个标记 stringtokenizer 不会返回空标记 我尝试过 string split 但我相信我做错了什么 String strings line split 29 示例输入 10150 15 58
  • 如何将 MSpec 与 MS Build 集成?

    前几天我看了一个BDD 截屏视频 by 罗布 康纳利 在视频中他展示了如何使用MSpec 所以我下载了它并玩了一下 我现在想要的是整合MSpec使用 MS Build 但我不知道如何 我使用 TFS team build 作为我的 CI 服
  • 正则表达式撇号如何匹配?

    我想添加到此规则匹配撇号 rule re compile r lt gt 我努力了 rule re compile r lt gt 但它认为撇号是行错误 为什么 错误的出现是因为你不能直接使用单个 inside 以及同样单身的 不能在里面使
  • 具有需要存在另一个参数的可选参数

    很简单 我如何初始化params我的 Powershell 脚本的一部分 这样我就可以有一个命令行参数 例如 Get Foo foo1
  • Oracle:创建具有自动增量 id 列的视图

    我创建了一个view填充来自不同表的数据 我用了10 select statements并使用组合这些选择语句的结果UNION ALL 我想添加primary key column以我看来 因为我必须创造XML使用此中的数据的文件view
  • 如何在 C# 中对位图图像应用模糊效果? [关闭]

    Closed 这个问题需要多问focused 目前不接受答案 如何在不使用库的情况下在 C 中对图像应用模糊效果 更新的代码 现在更快 需要使用 UNSAFE 关键字 static void Main string args Bitmap