解析C#彩色圖像灰度化算法的實(shí)現(xiàn)代碼詳解
更新時(shí)間:2013年05月20日 10:07:20 作者:
本篇文章是對(duì)C#中彩色圖像灰度化算法的實(shí)現(xiàn)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
代碼如下所示:
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;
}
復(fù)制代碼 代碼如下:
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;
}
相關(guān)文章
C# [ImportDll()] 知識(shí)小結(jié)
今天小編就為大家分享一篇關(guān)于C# [ImportDll()] 知識(shí)小結(jié),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01WPF+ASP.NET SignalR實(shí)現(xiàn)后臺(tái)通知功能的示例代碼
本文以一個(gè)簡單示例,簡述如何通過WPF+ASP.NET SignalR實(shí)現(xiàn)消息后臺(tái)通知以及數(shù)據(jù)的實(shí)時(shí)刷新,僅供學(xué)習(xí)分享使用,如有不足之處,還請(qǐng)指正2022-09-09C#開發(fā)微信門戶及應(yīng)用(2) 微信消息處理和應(yīng)答
文章主要為大家詳細(xì)介紹了C#開發(fā)微信門戶及應(yīng)用第二篇,微信消息處理和應(yīng)答,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06使用linq to xml修改app.config示例(linq讀取xml)
這篇文章主要介紹了使用linq to xml修改app.config示例,需要的朋友可以參考下2014-02-02C#中String.LastIndexOf方法小結(jié)
String.LastIndexOf()是C#中string類的一個(gè)方法,它用于在字符串中查找指定子字符串(或字符)最后一次出現(xiàn)的位置,并返回其索引,本文主要介紹了C#中String.LastIndexOf方法小結(jié),感興趣的可以了解一下2024-01-01C# 使用動(dòng)態(tài)庫DllImport("kernel32")讀寫ini文件的步驟
kernel32.dll是Windows中非常重要的32位動(dòng)態(tài)鏈接庫文件,屬于內(nèi)核級(jí)文件,這篇文章主要介紹了C# 利用動(dòng)態(tài)庫DllImport("kernel32")讀寫ini文件,需要的朋友可以參考下2023-05-05