C # intercepts graphics from a large image

Source: Internet
Author: User

#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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.