C # Image Processing: Image Scaling and cropping

Source: Internet
Author: User

According to the requirements of the audience for on-demand video, today we will talk about using C # To scale and crop images. I believe many people will be interested in this part of content. After all, this operation is too practical.

In fact, in GDI +, scaling and cropping can be seen as the same operation, but the selection of the original region is different. Just look at the detailsAlgorithmIt may be better understood.

/// <Summary> <br/> // resize image <br/> /// </Summary> <br/> /// <Param name = "BMP"> original bitmap </param> <br/> // <Param name = "NEWW"> New width </param> <br/> // <Param name = "newh"> new height </param> <br/> // <Param name = "Mode"> reserved, temporarily unavailable </param> <br/> // <returns> processed images </returns> <br/> Public static bitmap kiresizeimage (bitmap BMP, int NEWW, int newh, int mode) <br/>{< br/> try <br/>{< br/> bitmap B = new Bitmap (NEWW, newh ); <br/> graphics G = graphics. fromimage (B); </P> <p> // quality of the interpolation algorithm <br/> G. interpolationmode = interpolationmode. highqualitybicubic; </P> <p> G. drawimage (BMP, new rectangle (0, 0, NEWW, newh), new rectangle (0, 0, BMP. width, BMP. height), graphicsunit. pixel); <br/> G. dispose (); </P> <p> return B; <br/>}< br/> catch <br/>{< br/> return NULL; <br/>}</P> <p> // ===================== ============</P> <p> /// <summary> <br/> /// cropping-use GDI + <br/>/ /// </Summary> <br/> // <Param name = "B"> original bitmap </param> <br/> // <Param name = "startx "> Start coordinate x </param> <br/> // <Param name =" starty "> Start coordinate Y </param> <br/> // <Param name = "iwidth"> width </param> <br/> // <Param name = "iheight"> height </param> <br/> // <returns> cropped bitmap </returns> <br/> Public static bitmap kicut (Bitmap B, int startx, int starty, int iwidth, int iheight) <br/>{< br/> If (B = NULL) <br/>{< br/> return NULL; <br/>}</P> <p> int W = B. width; <br/> int H = B. height; </P> <p> If (startx> = w | starty> = h) <br/>{< br/> return NULL; <br/>}</P> <p> If (startx + iwidth> W) <br/>{< br/> iwidth = W-startx; <br/>}</P> <p> If (starty + iheight> H) <br/>{< br/> iheight = H-starty; <br/>}</P> <p> try <br/> {<br/> bitmap bmpout = new Bitmap (iwidth, iheight, pixelformat. format24bpprgb); </P> <p> graphics G = graphics. fromimage (bmpout); <br/> G. drawimage (B, new rectangle (0, 0, iwidth, iheight), new rectangle (startx, starty, iwidth, iheight), graphicsunit. pixel); <br/> G. dispose (); </P> <p> return bmpout; <br/>}< br/> catch <br/>{< br/> return NULL; <br/>}</P> <p>

Have you noticed the difference? Prompt: The second new rectangle in G. drawimage.

The targets are all new rectangle (0, 0, iwidth, iheight). The Scaling Algorithm inserts new rectangle (0, 0, BMP) into the target image. width, BMP. height), while cropping only inserts the new rectangle (startx, starty, iwidth, iheight) in the area with the top width and height of the original area into the target area. It's easy.

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.