溫馨提示×

溫馨提示×

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

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

C# Bitmap圖像飽和度調整方法

發(fā)布時間:2024-07-16 09:56:04 來源:億速云 閱讀:79 作者:小樊 欄目:編程語言

下面是一個簡單的示例,演示如何使用C#代碼調整Bitmap圖像的飽和度:

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

public class ImageProcessor
{
    public static Bitmap AdjustSaturation(Bitmap image, float saturationLevel)
    {
        Bitmap adjustedImage = new Bitmap(image.Width, image.Height);

        Graphics g = Graphics.FromImage(adjustedImage);

        ImageAttributes attributes = new ImageAttributes();
        ColorMatrix matrix = new ColorMatrix(new float[][]
        {
            new float[] {0.3086f*(1 + saturationLevel), 0.6094f*(1 - saturationLevel), 0.0820f*(1 - saturationLevel), 0, 0},
            new float[] {0.3086f*(1 - saturationLevel), 0.6094f*(1 + saturationLevel), 0.0820f*(1 - saturationLevel), 0, 0},
            new float[] {0.3086f*(1 - saturationLevel), 0.6094f*(1 - saturationLevel), 0.0820f*(1 + saturationLevel), 0, 0},
            new float[] {0, 0, 0, 1, 0},
            new float[] {0, 0, 0, 0, 1}
        });

        attributes.SetColorMatrix(matrix);

        g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height),
            0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);

        g.Dispose();

        return adjustedImage;
    }

    public static void Main()
    {
        Bitmap originalImage = new Bitmap("original.jpg");
        Bitmap adjustedImage = AdjustSaturation(originalImage, 0.5f);

        adjustedImage.Save("adjusted.jpg", ImageFormat.Jpeg);
    }
}

在這個示例中,AdjustSaturation方法接受一個Bitmap對象和一個saturationLevel參數(shù),該參數(shù)控制圖像的飽和度。方法使用Graphics類和ColorMatrix來應用顏色矩陣,從而實現(xiàn)飽和度的調整。最后,將調整后的圖像保存為JPEG格式。您可以根據(jù)需要調整saturationLevel參數(shù)的值來改變圖像的飽和度。

向AI問一下細節(jié)

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

AI