Gadgets: & simple image processing, tools:
1. program running
2. How to obtain the screen
First, we can use Screen. PrimaryScreen. Bounds to get the current Screen. Then we can use Bitmap and Graphics to get the picture of the Screen.
Screen. PrimaryScreen. WorkingArea. This Screen does not contain the taskbar.
The code for obtaining the screen is as follows:
1 /// <summary> 2 /// get Screen image 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); 8g. copyFromScreen (0, 0, 0, 0, new Size (Screen. primaryScreen. bounds. width, Screen. primaryScreen. bounds. height); 9}
The screen obtained in this way does not meet our requirements. What we need is that we can select our own region if we want QQ. This function will be available for further study.
Iii. Simple Image Processing
After obtaining an image, we always want to process it to achieve different effects. To achieve some results, we need to modify each pixel of the image.
3.1 black/white Effects
Implementation Method:
1 /// <summary> 2 // black/white effect 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. weighted algorithm); 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} 2 0 21 /// <summary> 22 // black/white algorithm 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. weighted algorithm: 33 result = (int) (0.7 * pixel. r) + (int) (0.2 * pixel. g) + (int) (0.1 * pixel. b); 34 break; 35 case EnumUtil. calculate. Average Value: 36 result = (pixel. R + pixel. G + pixel. b)/3; 37 break; 38 case EnumUtil. calculate. maximum Value: 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 Negative Effect
Implementation Method:
1 /// <summary> 2 /// negative effect 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 embossed Effect
Implementation Method:
1 /// <summary> 2 /// embossed effect 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 // Relief algorithm 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. embossed: 34 r = Math. abs (pixel. r-pixe L2.R + 128)> 255? 255: Math. Abs (pixel. R-pixel2.R + 128); 35g = 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. relief: 39 r = Math. abs (pixel2.R-pixel. R + 128)> 255? 255: Math. Abs (pixel2.R-pixel. R + 128); 40g = 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}