您好,登錄后才能下訂單哦!
下面是一個簡單的示例,演示如何使用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
參數的值來改變圖像的飽和度。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。