#region Intercept image///<summary>///to intercept a part of a picture to generate a new diagram///</summary>///<param name= "Sfromfilepath" > Original picture < /param>///<param name= "Savefilepath" > Generate a new map </param>///<param name= "width" > Intercept picture width </param& Gt <param name= "height" > Intercept picture height </param>///<param name= "SpaceX" > Screenshot picture x coordinate </param>///<p Aram Name= "SpaceY" > Intercept picture y-coordinate </param> public static void Captureimage (String sfromfilepath, String Savefilepath, int width, int height, int spaceX, int spaceY) {//load bottom image fromimage = Image.FromFile (sfromfilepath); int x = 0;//intercept X coordinate int y = 0; Intercept y-coordinate//Huantuquan and generate picture width//when less than 0 (that is, the original image is less than the graph to be generated), the new chart has a smaller width of the original width x coordinate is 0//when greater than 0 (that is, the original image is larger than the graph to be generated), the new chart width is set value, that is, width x coordinate is SX and SpaceX the smaller//y direction of empathy int sX = fromimage.width-width; int SY = Fromimage.height-height; if (SX > 0) {x = SX > SpaceX? spacex:sx} else {width = fromimage.width} if (Sy > 0) {y = sy > SpaceY ? Spacey:sy; else {height = Fromimage.heigHt //Create a new diagram bitmap Bitmap Bitmap = Bitmap (width, height); Create a drawing area Graphics graphic = graphics.fromimage (bitmap); The corresponding area of the original image is written into the mapping area graphic. DrawImage (fromimage, 0, 0, New Rectangle (x, y, width, height), graphicsunit.pixel); Generates a new graph image saveimage = Image.fromhbitmap (bitmap) from the drawing area. Gethbitmap ()); Save the image Saveimage.save (Savefilepath, imageformat.jpeg); Releasing resources saveimage.dispose (); Bitmap. Dispose (); Graphic. Dispose (); } #endregion
//Call method
Captureimage (@ "d:/1.jpg", @ "D:/2.jpg", 50, 50, 300, 300);