溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2024-07-16 10:02:06 來源:億速云 閱讀:95 作者:小樊 欄目:編程語言

在C#中實現(xiàn)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對象和模糊程度參數(shù)blurAmount,然后對輸入的圖像進行模糊處理并返回模糊后的圖像。最后在使用示例中可以加載原始圖像,應(yīng)用模糊效果并保存為新的圖像文件。模糊效果的強度取決于blurAmount參數(shù)的值,可以根據(jù)需求進行調(diào)整。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI