温馨提示×

温馨提示×

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

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

C# Bitmap图像饱和度调整方法

发布时间: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参数,该参数控制图像的饱和度。方法使用Graphics类和ColorMatrix来应用颜色矩阵,从而实现饱和度的调整。最后,将调整后的图像保存为JPEG格式。您可以根据需要调整saturationLevel参数的值来改变图像的饱和度。

向AI问一下细节

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

AI