C# RGB圖像和灰度圖像互轉(zhuǎn)的實(shí)現(xiàn)
RGB圖像轉(zhuǎn)為灰度圖像
using System; using System.Drawing; using System.Drawing.Imaging; namespace ConsoleApp { class Program { static void Main(string[] args) { // 創(chuàng)建RGB圖像 Image img = new Bitmap("RGB圖像路徑"); // 獲取RGB圖像的Width和Height int width = img.Width; int height = img.Height; // 創(chuàng)建灰度圖像 Image grayImg = new Bitmap(width, height); // 獲取灰度圖像的BytesPerPixel int grayBytesPerPixel = grayImg.GetPixelFormatSize(Color.Format32bppArgb); // 計(jì)算灰度圖像的總像素?cái)?shù) int grayPixelCount = width * height; // 遍歷RGB圖像的每個(gè)像素,將其轉(zhuǎn)為灰度值并寫入灰度圖像 for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color c = img.GetPixel(x, y); int r = (int)(c.R / 255 * 255); int g = (int)(c.G / 255 * 255); int b = (int)(c.B / 255 * 255); int gray = (r + g + b) / 3; grayImg.SetPixel(x, y, Color.FromArgb(gray)); } } // 顯示灰度圖像 grayImg.Save("灰度圖像路徑"); } } }
灰度圖像轉(zhuǎn)為RGB圖像
using System; using System.Drawing; using System.Drawing.Imaging; namespace ConsoleApp { class Program { static void Main(string[] args) { // 創(chuàng)建灰度圖像 Image img = new Bitmap("灰度圖像路徑"); // 獲取灰度圖像的Width和Height int width = img.Width; int height = img.Height; // 創(chuàng)建RGB圖像 Image rgbImg = new Bitmap(width, height); // 獲取RGB圖像的BytesPerPixel int rgbBytesPerPixel = rgbImg.GetPixelFormatSize(Color.Format32bppArgb); // 計(jì)算RGB圖像的總像素?cái)?shù) int rgbPixelCount = width * height; // 遍歷灰度圖像的每個(gè)像素,將其轉(zhuǎn)為RGB值并寫入RGB圖像 for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color c = img.GetPixel(x, y); int gray = c.R; rgbImg.SetPixel(x, y, Color.FromArgb(gray, gray, gray)); } } // 顯示RGB圖像 rgbImg.Save("RGB圖像路徑"); } } }
到此這篇關(guān)于C# RGB圖像和灰度圖像互轉(zhuǎn)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C# RGB圖像和灰度圖像互轉(zhuǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中調(diào)用命令行cmd開啟wifi熱點(diǎn)的實(shí)例代碼
最近想在win7上開啟wifi熱點(diǎn),于是就弄出下面這個(gè)小東西,里面涉及如何在控制臺(tái)上輸入命令,分享一下。首先在VS中創(chuàng)建一個(gè)window窗口,然后創(chuàng)建兩個(gè)四個(gè)button,兩個(gè)輸入框2013-04-04C# 定時(shí)器定時(shí)更新的簡(jiǎn)單實(shí)例
這篇文章主要介紹了C#中定時(shí)器定時(shí)更新的簡(jiǎn)單實(shí)例。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-12-12.NET/C#實(shí)現(xiàn)識(shí)別用戶訪問設(shè)備的方法
這篇文章主要介紹了.NET/C#實(shí)現(xiàn)識(shí)別用戶訪問設(shè)備的方法,結(jié)合實(shí)例形式分析了C#識(shí)別用戶訪問設(shè)備的操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02c# DataTable與不同結(jié)構(gòu)實(shí)體類轉(zhuǎn)換的方法實(shí)例
這篇文章主要介紹了c#的DataTable與不同結(jié)構(gòu)實(shí)體類轉(zhuǎn)換的方法實(shí)例,在大數(shù)據(jù)量的情況下很實(shí)用,大家可以參考使用2013-11-11