小工具:截圖&簡單圖像處理
阿新 • • 發佈:2017-08-18
math rgb 最大值 3-9 width work 小工具 case int
一、程序運行截圖
二、獲取屏幕截圖的方法
首先知道我們可以通過Screen.PrimaryScreen.Bounds獲取到當前整個屏幕,再利用Bitmap和Graphics就可以得到整個屏幕的圖片了。
Screen.PrimaryScreen.WorkingArea這個獲得是不包含任務欄的屏幕
獲取屏幕代碼如下所示:
1 /// <summary> 2 /// 獲取屏幕圖片 3 /// </summary> 4 private void GetScreenImage() 5{ 6 Bitmap bitMap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); 7 Graphics g = Graphics.FromImage(bitMap); 8 g.CopyFromScreen(0, 0, 0, 0, new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));9 }
這樣獲得的屏幕截圖並不能滿足我們的要求,我們需要的是要想QQ那樣的可以可以自己選區域的截圖,這個功能待以後有時間再研究如何實現。
三、簡單圖像處理
獲得圖片後,我們總是想要對它進行一些處理,實現不同的效果。為了實現一些效果,我們需要對圖片的每個像素進行修改。
3.1 黑白效果
實現方法:
1 /// <summary> 2 /// 黑白效果 3 /// </summary> 4 public Bitmap ImgBlackWhite(Bitmap bitmap)5 { 6 for (int i = 1; i < bitmap.Width; i++) 7 { 8 for (int j = 1; j < bitmap.Height; j++) 9 { 10 Color pixel = bitmap.GetPixel(i, j); 11 int avg = GetBWNum(pixel, EnumUtil.Calculate.加權算法); 12 int r = avg; 13 int g = avg; 14 int b = avg; 15 bitmap.SetPixel(i, j, Color.FromArgb(r, g, b)); 16 } 17 } 18 return bitmap; 19 } 20 21 /// <summary> 22 /// 黑白效果算法 23 /// </summary> 24 /// <param name="pixel"></param> 25 /// <param name="calcul"></param> 26 /// <returns></returns> 27 private int GetBWNum(Color pixel, EnumUtil.Calculate calcul) 28 { 29 int result = 0; 30 switch (calcul) 31 { 32 case EnumUtil.Calculate.加權算法: 33 result = ((int)(0.7 * pixel.R) + (int)(0.2 * pixel.G) + (int)(0.1 * pixel.B)); 34 break; 35 case EnumUtil.Calculate.平均值: 36 result = (pixel.R + pixel.G + pixel.B) / 3; 37 break; 38 case EnumUtil.Calculate.最大值: 39 result = pixel.R > pixel.G ? pixel.R : pixel.G; 40 result = result > pixel.B ? result : pixel.B; 41 break; 42 } 43 return result; 44 }
3.2 負片效果
實現方法:
1 /// <summary> 2 /// 負片效果 3 /// </summary> 4 public Bitmap ImgNagative(Bitmap bitmap) 5 { 6 for (int i = 1; i < bitmap.Width; i++) 7 { 8 for (int j = 1; j < bitmap.Height; j++) 9 { 10 Color c = bitmap.GetPixel(i, j); 11 12 int r = 255 - c.R; 13 int g = 255 - c.G; 14 int b = 255 - c.B; 15 bitmap.SetPixel(i, j, Color.FromArgb(r, g, b)); 16 } 17 } 18 return bitmap; 19 }
3.3 浮雕效果
實現方法:
1 /// <summary> 2 /// 浮雕效果 3 /// </summary> 4 public Bitmap ImgCameo(Bitmap bitmap, EnumUtil.ImageStyle style) 5 { 6 Color pixel, pixel2; 7 8 for (int i = 0; i < bitmap.Width - 1; i++) 9 { 10 for (int j = 0; j < bitmap.Height - 1; j++) 11 { 12 pixel = bitmap.GetPixel(i, j); 13 pixel2 = bitmap.GetPixel(i + 1, j + 1); 14 bitmap.SetPixel(i, j, ImgCameoCalcul(pixel, pixel2, style)); 15 } 16 } 17 return bitmap; 18 } 19 20 /// <summary> 21 /// 浮雕算法 22 /// </summary> 23 /// <param name="pixel"></param> 24 /// <param name="pixel2"></param> 25 /// <param name="style"></param> 26 /// <returns></returns> 27 private Color ImgCameoCalcul(Color pixel, Color pixel2, EnumUtil.ImageStyle style) 28 { 29 Color cResult; 30 int r = 0, g = 0, b = 0; 31 switch (style) 32 { 33 case EnumUtil.ImageStyle.浮雕陰刻: 34 r = Math.Abs(pixel.R - pixel2.R + 128) > 255 ? 255 : Math.Abs(pixel.R - pixel2.R + 128); 35 g = Math.Abs(pixel.G - pixel2.G + 128) > 255 ? 255 : Math.Abs(pixel.G - pixel2.G + 128); 36 b = Math.Abs(pixel.B - pixel2.B + 128) > 255 ? 255 : Math.Abs(pixel.B - pixel2.B + 128); 37 break; 38 case EnumUtil.ImageStyle.浮雕陽刻: 39 r = Math.Abs(pixel2.R - pixel.R + 128) > 255 ? 255 : Math.Abs(pixel2.R - pixel.R + 128); 40 g = Math.Abs(pixel2.G - pixel.G + 128) > 255 ? 255 : Math.Abs(pixel2.G - pixel.G + 128); 41 b = Math.Abs(pixel2.B - pixel.B + 128) > 255 ? 255 : Math.Abs(pixel2.B - pixel.B + 128); 42 break; 43 } 44 cResult = Color.FromArgb(r, g, b); 45 return cResult; 46 }
小工具:截圖&簡單圖像處理