解析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#動(dòng)態(tài)編譯并執(zhí)行字符串樣例
這篇文章主要為大家詳細(xì)介紹了C#動(dòng)態(tài)編譯并執(zhí)行字符串樣例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
C#實(shí)現(xiàn)最簡(jiǎn)單的文本加密方法
這篇文章主要介紹了C#實(shí)現(xiàn)最簡(jiǎn)單的文本加密方法,可實(shí)現(xiàn)簡(jiǎn)單的文本加密功能,是非常實(shí)用的技巧,需要的朋友可以參考下2014-12-12
WPF+SkiaSharp實(shí)現(xiàn)自繪投籃小游戲
這篇文章主要介紹了如何利用WPF+SkiaSharp實(shí)現(xiàn)自繪投籃小游戲。此案例主要是針對(duì)光線投影法碰撞檢測(cè)功能的示例,順便做成了一個(gè)小游戲,很簡(jiǎn)單,但是,效果卻很不錯(cuò),感興趣的可以動(dòng)手嘗試一下2022-08-08
C# 文件操作函數(shù) 創(chuàng)建文件 判斷存在
本文列舉了C#中文件操作中常用的函數(shù),創(chuàng)建文件和判斷文件存不存在的基本使用,簡(jiǎn)單實(shí)用,希望能幫到大家。2016-05-05
判斷一個(gè)整數(shù)是否是2的N次冪實(shí)現(xiàn)方法
下面小編就為大家分享一篇判斷一個(gè)整數(shù)是否是2的N次冪實(shí)現(xiàn)方法,實(shí)例簡(jiǎn)潔,具有很好的參考價(jià)值。希望對(duì)大家有所幫助2017-11-11
C#簡(jiǎn)單實(shí)現(xiàn)防止多個(gè)程序運(yùn)行的方法
這篇文章主要介紹了C#簡(jiǎn)單實(shí)現(xiàn)防止多個(gè)程序運(yùn)行的方法,涉及C#進(jìn)程操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-02-02

