解析C#彩色圖像灰度化算法的實現(xiàn)代碼詳解
更新時間:2013年05月20日 10:07:20 作者:
本篇文章是對C#中彩色圖像灰度化算法的實現(xiàn)進行了詳細的分析介紹,需要的朋友參考下
代碼如下所示:
public static Bitmap MakeGrayscale(Bitmap original)
{
//create a blank bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);
//get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);
//create the grayscale ColorMatrix
System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(
new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
//create some image attributes
System.Drawing.Imaging.ImageAttributes attributes = new System.Drawing.Imaging.ImageAttributes();
//set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);
//draw the original image on the new image
//using the grayscale color matrix
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
//dispose the Graphics object
g.Dispose();
return newBitmap;
}
復制代碼 代碼如下:
public static Bitmap MakeGrayscale(Bitmap original)
{
//create a blank bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);
//get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);
//create the grayscale ColorMatrix
System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(
new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
//create some image attributes
System.Drawing.Imaging.ImageAttributes attributes = new System.Drawing.Imaging.ImageAttributes();
//set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);
//draw the original image on the new image
//using the grayscale color matrix
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
//dispose the Graphics object
g.Dispose();
return newBitmap;
}
相關文章
WPF+ASP.NET SignalR實現(xiàn)后臺通知功能的示例代碼
本文以一個簡單示例,簡述如何通過WPF+ASP.NET SignalR實現(xiàn)消息后臺通知以及數(shù)據(jù)的實時刷新,僅供學習分享使用,如有不足之處,還請指正2022-09-09使用linq to xml修改app.config示例(linq讀取xml)
這篇文章主要介紹了使用linq to xml修改app.config示例,需要的朋友可以參考下2014-02-02C#中String.LastIndexOf方法小結(jié)
String.LastIndexOf()是C#中string類的一個方法,它用于在字符串中查找指定子字符串(或字符)最后一次出現(xiàn)的位置,并返回其索引,本文主要介紹了C#中String.LastIndexOf方法小結(jié),感興趣的可以了解一下2024-01-01C# 使用動態(tài)庫DllImport("kernel32")讀寫ini文件的步驟
kernel32.dll是Windows中非常重要的32位動態(tài)鏈接庫文件,屬于內(nèi)核級文件,這篇文章主要介紹了C# 利用動態(tài)庫DllImport("kernel32")讀寫ini文件,需要的朋友可以參考下2023-05-05