Bitmap图像在C#中的模糊效果

发布时间:2024-07-16 10:02:06 作者:小樊
来源:亿速云 阅读:94

在C#中实现Bitmap图像的模糊效果可以使用以下代码示例:

using System;
using System.Drawing;
using System.Drawing.Imaging;

public static class BitmapExtensions
{
    public static Bitmap ApplyBlur(this Bitmap sourceImage, int blurAmount)
    {
        Bitmap blurredImage = new Bitmap(sourceImage.Width, sourceImage.Height);

        using (Graphics graphics = Graphics.FromImage(blurredImage))
        {
            graphics.DrawImage(sourceImage, new Rectangle(0, 0, sourceImage.Width, sourceImage.Height),
                new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), GraphicsUnit.Pixel);
        }

        for (int i = 0; i < blurAmount; i++)
        {
            using (Bitmap tempImage = new Bitmap(blurredImage))
            {
                using (Graphics graphics = Graphics.FromImage(blurredImage))
                {
                    graphics.DrawImage(tempImage, new Rectangle(0, 0, tempImage.Width, tempImage.Height),
                        new Rectangle(0, 0, tempImage.Width, tempImage.Height), GraphicsUnit.Pixel);
                }
            }
        }

        return blurredImage;
    }
}

// 使用示例
Bitmap sourceImage = new Bitmap("image.jpg");
int blurAmount = 5;
Bitmap blurredImage = sourceImage.ApplyBlur(blurAmount);
blurredImage.Save("blurred_image.jpg", ImageFormat.Jpeg);

在上面的代码中,首先定义了一个扩展方法ApplyBlur,该方法接受一个Bitmap对象和模糊程度参数blurAmount,然后对输入的图像进行模糊处理并返回模糊后的图像。最后在使用示例中可以加载原始图像,应用模糊效果并保存为新的图像文件。模糊效果的强度取决于blurAmount参数的值,可以根据需求进行调整。

推荐阅读:
  1. 如何用CMS设置网站模板风格
  2. springboot中怎么配置AutoConfiguration

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:C# Bitmap图像锐化处理技术

下一篇:C# Bitmap图像特效实现方式

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》