本篇文章給大家分享的是有關(guān)如何在C#中處理圖像,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
創(chuàng)新互聯(lián)公司是一家專注于網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)與策劃設(shè)計(jì),牟定網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:牟定等地區(qū)。牟定做網(wǎng)站價(jià)格咨詢:18982081108(1)在Form1窗體中的PictureBox1控件中顯示通過(guò)OpenFileDialog指定的圖像文件內(nèi)容。
將SizeMode設(shè)置成StretchImage
private void 打開ToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog open = new OpenFileDialog(); open.Filter = "所有文件|*.*"; if (open.ShowDialog() == DialogResult.OK) { Bitmap im = new Bitmap(open.FileName); pictureBox1.Image = im; //pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; } }
(2)編制一個(gè)圖像格式轉(zhuǎn)換的應(yīng)用程序,實(shí)現(xiàn)BMP圖像與JPEG圖像的格式轉(zhuǎn)換。
private void bMPToolStripMenuItem_Click(object sender, EventArgs e) { Bitmap box = new Bitmap(pictureBox1.Image); SaveFileDialog sv1 = new SaveFileDialog(); sv1.Filter = "bmp|*.bmp"; sv1.ShowDialog(); string str = sv1.FileName; box.Save(str, System.Drawing.Imaging.ImageFormat.Bmp); } private void jPEGToolStripMenuItem_Click(object sender, EventArgs e) { Bitmap box = new Bitmap(pictureBox1.Image); SaveFileDialog sv1 = new SaveFileDialog(); sv1.Filter = "jpeg文件|*.jpeg"; sv1.ShowDialog(); string str = sv1.FileName; box.Save(str, System.Drawing.Imaging.ImageFormat.Jpeg); }
(3)在窗體中添加兩個(gè)PictureBox,然后利用OpenFileDialog控件選擇一彩色圖像文件并在pictureBox1中加載,然后將這一彩色圖像轉(zhuǎn)換為灰度圖像,并在pictureBox2中進(jìn)行顯示,將此圖像保存為a.bmp。
private void 灰度ToolStripMenuItem_Click(object sender, EventArgs e) { pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; Color c = new Color(); Bitmap b1 = new Bitmap(pictureBox1.Image); int rr, gg, bb, cc; for (int i = 0; i < pictureBox1.Image.Width; i++) { for (int j = 0; j < pictureBox1.Image.Height; j++) { c = b1.GetPixel(i, j); rr = c.R; gg = c.G; bb = c.B; cc = (int)((rr + gg + bb) / 3); if (cc < 0) cc = 0; if (cc > 255) cc = 255; Color nc = Color.FromArgb(cc, cc, cc); b1.SetPixel(i, j, nc); } } pictureBox2.Refresh(); pictureBox2.Image = b1; } private void 灰度圖像存儲(chǔ)ToolStripMenuItem_Click(object sender, EventArgs e) { if (pictureBox2.Image != null) { Bitmap box = new Bitmap(pictureBox2.Image); SaveFileDialog sv1 = new SaveFileDialog(); sv1.Filter = "bmp文件|*.bmp"; sv1.ShowDialog(); string str = sv1.FileName; box.Save(str, System.Drawing.Imaging.ImageFormat.Bmp); //pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; } else MessageBox.Show("沒(méi)有生成灰度圖像"); }
(4)將(3)中制作的a.bmp圖像加載到PictureBox1,把a(bǔ).bmp轉(zhuǎn)換為二值圖像,并在pictureBox2中進(jìn)行顯示。
private void 轉(zhuǎn)化ToolStripMenuItem_Click(object sender, EventArgs e) { //Bitmap m = new Bitmap(pictureBox1.Width, pictureBox1.Height); Bitmap map=new Bitmap(pictureBox1.Image); Color c; for (int i = 0; i < map.Height; i++) { for (int j = 0; j < map.Width; j++) { c = map.GetPixel(j, i); if (c.R > 100) c = Color.FromArgb(255, 255, 255); //紅色分量值大于100則設(shè)成白色 else c = Color.Black; map.SetPixel(j, i, c); } } pictureBox2.Image = map; pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; }
(5)建立對(duì)比度增強(qiáng)算法,并對(duì)a.bmp圖像進(jìn)行對(duì)比度增強(qiáng),顯示在pictureBox2中顯示出來(lái),與PictureBox1中的a.bmp圖像進(jìn)行比較。
private void 對(duì)比ToolStripMenuItem_Click(object sender, EventArgs e) { Color c = new Color(); Bitmap box1 = new Bitmap(pictureBox1.Image); int lev=30, wid=170; int[] map = new int[256]; for(int i=0;i<=lev;i++) { map[i]=0; } for (int i=lev;i<=wid;i++) //對(duì)比度增強(qiáng) { map[i] = (int)((i * 1.0 - lev) / wid * 255); } for (int i = lev + wid; i < 256; i++) { map[i] = 255; } int gray; for (int i = 0; i < box1.Width; i++) { for (int j = 0; j < box1.Height; j++) { c = box1.GetPixel(i, j); gray = c.R; c = Color.FromArgb(map[gray], map[gray], map[gray]); box1.SetPixel(i, j, c); } } pictureBox2.Image = box1; pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; }
C#是一個(gè)簡(jiǎn)單、通用、面向?qū)ο蟮木幊陶Z(yǔ)言,它由微軟Microsoft開發(fā),繼承了C和C++強(qiáng)大功能,并且去掉了一些它們的復(fù)雜特性,C#綜合了VB簡(jiǎn)單的可視化操作和C++的高運(yùn)行效率,以其強(qiáng)大的操作能力、優(yōu)雅的語(yǔ)法風(fēng)格、創(chuàng)新的語(yǔ)言特性和便捷的面向組件編程從而成為.NET開發(fā)的選語(yǔ)言,但它不適用于編寫時(shí)間急迫或性能非常高的代碼,因?yàn)镃#缺乏性能極高的應(yīng)用程序所需要的關(guān)鍵功能。
以上就是如何在C#中處理圖像,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。