【C#】圖片處理——浮雕、黑白、油畫效果原始碼
阿新 • • 發佈:2020-04-29
本例應用場合:對圖片進行基本的翻轉操作,圖片浮雕、黑白、油畫效果的實現。
本例編譯環境:VS2010及以上版本編譯通過。
執行效果:
主要功能:
-
圖片浮雕、黑白、油畫效果處理;
-
圖片翻轉處理——旋轉180°,順時針、逆時針旋轉90°,垂直、水平翻轉;
-
圖片儲存、另存為。
主要程式碼:
1 /// <summary> 2 /// 浮雕效果 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param>View Code6 private void btnFudiao_Click(object sender,EventArgs e) 7 { 8 label1.Text = "正在完成圖片操作,可能會出現程式未響應的情況。請稍等一段時間,如果等待時間過長,請重新啟動程式!"; 9 shangyibu = new Bitmap(pictureBox1.Image); 10 Thread.Sleep(500); 11 try 12 { 13 Bitmap newBitmap = new Bitmap(intWidth,intHeight);14 Bitmap oldBitmap = (Bitmap)this.pictureBox1.Image; 15 Color pixel1,pixel2; 16 for (int x = 0; x < intWidth - 1; x++) 17 { 18 for (int y = 0; y < intHeight - 1; y++) 19 { 20 intr = 0,g = 0,b = 0; 21 pixel1 = oldBitmap.GetPixel(x,y); 22 pixel2 = oldBitmap.GetPixel(x + 1,y + 1); 23 r = Math.Abs(pixel1.R - pixel2.R + 128); 24 g = Math.Abs(pixel1.G - pixel2.G + 128); 25 b = Math.Abs(pixel1.B - pixel2.B + 128); 26 if (r > 255) 27 { 28 r = 255; 29 } 30 if (r < 0) 31 { 32 r = 0; 33 } 34 if (g > 255) 35 { 36 g = 255; 37 } 38 if (g < 0) 39 { 40 g = 0; 41 } 42 if (b > 255) 43 { 44 b = 255; 45 } 46 if (b < 0) 47 { 48 b = 0; 49 } 50 newBitmap.SetPixel(x,y,Color.FromArgb(r,g,b)); 51 } 52 } 53 this.pictureBox1.Image = newBitmap; 54 label1.Text = "已完成圖片操作:“浮雕”!"; 55 save = 1; 56 this.Text = "*圖片處理——" + open; 57 } 58 catch (Exception ex) 59 { 60 MessageBox.Show(ex.Message); 61 } 62 }
1 /// <summary> 2 /// 黑白效果 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void btnHeibai_Click(object sender,EventArgs e) 7 { 8 label1.Text = "正在完成圖片操作,可能會出現程式未響應的情況。請稍等一段時間,如果等待時間過長,請重新啟動程式!"; 9 shangyibu = new Bitmap(pictureBox1.Image); 10 Thread.Sleep(500); 11 try 12 { 13 Bitmap newBitmap = new Bitmap(intWidth,intHeight); 14 Bitmap oldBitmap = (Bitmap)this.pictureBox1.Image; 15 Color pixel; 16 for (int x = 0; x < intWidth; x++) 17 { 18 for (int y = 0; y < intHeight; y++) 19 { 20 pixel = oldBitmap.GetPixel(x,y); 21 int r,b,Result = 0; 22 r = pixel.R; 23 g = pixel.G; 24 b = pixel.B; 25 int iType = 2; 26 switch (iType) 27 { 28 case 0: 29 Result = ((r + g + b) / 3); 30 break; 31 case 1: 32 Result = r > g ? r : g; 33 Result = Result > b ? Result : b; 34 break; 35 case 2: 36 Result = ((int)(0.7 * r) + (int)(0.2 * g) + (int)(0.1 * b)); 37 break; 38 } 39 newBitmap.SetPixel(x,Color.FromArgb(Result,Result,Result)); 40 } 41 } 42 this.pictureBox1.Image = newBitmap; 43 label1.Text = "已完成圖片操作:“黑白”!"; 44 save = 1; 45 this.Text = "*圖片處理——" + open; 46 } 47 catch (Exception ex) 48 { 49 MessageBox.Show(ex.Message); 50 } 51 }View Code
1 /// <summary> 2 /// 儲存圖片 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void saveToolStripMenuItem_Click(object sender,EventArgs e) 7 { 8 System.IO.FileStream picture = null; 9 try 10 { 11 Bitmap bmp = new Bitmap(pictureBox1.Image); 12 s = new Bitmap(pictureBox1.Image); 13 bmp.Save(open); 14 MessageBox.Show("儲存成功"); 15 label1.Text = "儲存成功!"; 16 this.Text = "圖片處理——" + open; 17 baocun = new Bitmap(pictureBox1.Image); 18 save = 0; 19 save = 0; 20 this.Text = "圖片處理——" + open; 21 } 22 catch (Exception ex) 23 { 24 MessageBox.Show(ex.Message); 25 label1.Text = "儲存失敗!"; 26 } 27 finally 28 { 29 if (picture != null) 30 { 31 picture.Close(); 32 picture.Dispose(); 33 } 34 } 35 } 36View Code
1 /// <summary> 2 /// 還原圖片 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void btnHuanyuan_Click(object sender,EventArgs e) 7 { 8 DialogResult aaa = MessageBox.Show("你確定要還原為未修改過的原照片嗎?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Asterisk); 9 if (aaa == DialogResult.Yes) 10 { 11 pictureBox1.Image = huanyuan; 12 13 if (pictureBox1.Image != s) 14 { 15 save = 1; 16 this.Text = "*圖片處理——" + open; 17 } 18 else 19 { 20 save = 0; 21 this.Text = "圖片處理——" + open; 22 } 23 } 24 }View Code
圖片的開啟、儲存、翻轉功能詳見原始碼。
原始碼免費下載地址請關注公眾號【幾行簡碼】後回覆【圖片處理】獲取。
原創文章,轉載請閱讀公眾號內轉載須知。
點關注,不迷路。