C # process irregular image Cropping

Source: Internet
Author: User
Tags transparent image

The latest project requires irregular cropping. I originally wanted to use the html5 canvas to implement it at the front end, but it was a bit difficult to find out. The following section describes how to process the image on the C # side. Now, you can paste the following code as a button event to prepare the image, crop the path, and save the private void button#click (object sender, EventArgs e) for cropping) {GraphicsPath path = new GraphicsPath (); Point [] p = {new Point (424,244), new Point (240,440), new Point (340,552), new Point (550,526 ), new Point (478,366), new Point (348,660), new Point (424,244)}; path. addLines (p); OpenFileDialog openFileDialog = new OpenFileDialog (); openFileDialog. filter = @ "Bitmap file (*. Bmp) | *. bmp | Jpeg file (*. jpg) | *. jpg | all suitable files (*. bmp ,*. jpg) | *. bmp ;*. jpg "; openFileDialog. filterIndex = 3; openFileDialog. restoreDirectory = true; if (DialogResult. OK = openFileDialog. showDialog () {Bitmap bit = (Bitmap) Bitmap. fromFile (openFileDialog. fileName, false); Bitmap newBit = null; BitmapCrop (bit, path, out newBit); newBit. save (@ "C: \ Users \ Public \ Pictures \ Sample Pictures \ desert 22.jpg") ;}} The following are the key to cropping. Code /// <summary> /// image /// </summary> /// <param name = "bitmap"> source image </param> /// <param name = "path"> cropping path </param> /// <param name = "outputBitmap"> output graph </param> /// <returns> </returns> public static bitmap BitmapCrop (Bitmap bitmap, graphicsPath path, out Bitmap outputBitmap) {RectangleF rect = path. getBounds (); int left = (int) rect. left; int top = (int) rect. top; int width = (int) rect. width; int height = (Int) rect. height; Bitmap image = (Bitmap) bitmap. clone (); outputBitmap = new Bitmap (width, height); for (int I = left; I <left + width; I ++) {for (int j = top; j <top + height; j ++) {// determine whether the coordinates are in the path if (path. isVisible (I, j) {// copy the pixels in the source image area to the outputBitmap of the output image. setPixel (I-left, j-top, image. getPixel (I, j); // set the original image to a transparent image. setPixel (I, j, Color. fromArgb (0, image. getPixel (I, j);} else {outp UtBitmap. setPixel (I-left, j-top, Color. fromArgb (0,255,255,255) ;}} bitmap. dispose (); return image;} with this foreground, you only need to obtain the user's mouse path and upload it to the background.

Related Article

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.