C # cropping a picture

Source: Internet
Author: User

Specific what, I do not understand, I know this is OK. You can study it when you are free.

The function is: crop the picture, but keep the original size of the picture, the cropped area is filled with the specified color (white), and then saved to another new folder, the name does not change.

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Drawing.Imaging;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespacewfimg{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private voidButton1_Click (Objectsender, EventArgs e) {FolderBrowserDialog Dialog=NewFolderBrowserDialog (); Dialog. Description="Please select a file path"; if(dialog. ShowDialog () = =DialogResult.OK) {stringFoldpath =dialog.                SelectedPath;  This. TextBox1.Text =Foldpath; }        }        /// <summary>        ///Trim/// </summary>        /// <param name= "B" >Original Bitmap</param>        /// <param name= "StartX" >Start coordinate x</param>        /// <param name= "Starty" >Start coordinate y</param>        /// <param name= "iwidth" >width</param>        /// <param name= "iheight" >Height</param>        /// <returns>the bitmap after clipping</returns>         Public StaticBitmap Kicut (Bitmap B,intStartX,intStarty,intIwidth,intiheight) {            if(b = =NULL)            {                return NULL; }            intW =B.width; inth =B.height; if(StartX >= W | | Starty >=h) {return NULL; }            if(StartX + iwidth >W) {iwidth= W-StartX; }            if(Starty + iheight >h) {iheight= h-Starty; }            Try            {                //Bitmap bmpout = new Bitmap (iwidth, iheight, Pixelformat.format24bpprgb);//a picture that is cropped to a smaller sizeBitmap bmpout=NewBitmap (W, H, Pixelformat.format24bpprgb);//keep the original size, the cropping section fills with the specified colorGraphics graphics =graphics.fromimage (bmpout); Graphics. Clear (color.white);//The cropping section fills with the specified colorGraphics. DrawImage (b,NewRectangle (StartX, Starty, iwidth, iheight),NewRectangle (StartX, Starty, iwidth, iheight), GraphicsUnit.Pixel); Graphics.                Dispose (); returnbmpout; }            Catch            {                return NULL; }        }        Private voidButton2_Click (Objectsender, EventArgs e) {            intLeftsize =Convert.ToInt32 (Txtleft.text); intTopsize =Convert.ToInt32 (Txttop.text); intBottomsize =Convert.ToInt32 (Txtbottom.text); intRightsize =Convert.ToInt32 (Txtright.text); stringFoldpath =TextBox1.Text; stringNewPath = Foldpath +"_new"; if(!directory.exists (NewPath))            {directory.createdirectory (NewPath); }            //loop files in this directoryDirectoryInfo dir =NewDirectoryInfo (Foldpath); fileinfo[] Files=dir.            GetFiles (); intFcount =files.            Count (); if(Fcount >0)            {                 for(inti =0; i < Fcount; i++) {FileInfo file=Files[i]; Bitmap Map=NewBitmap (file.                    FullName); intwidth =map.                    Width; intHeight =map.                    Height; Map= Kicut (map, Leftsize, Topsize, Width-rightsize-leftsize, Height-bottomsize-topsize); Map. Save (NewPath+"\\"+file.                    Name); Label6. Text="Execution Result: Success"+ (i +1) +"a"; }            }        }    }}

The interface is what this bird looks like:

The upper and lower left and right are pixel values.

C # cropping a picture

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.