C # proportional scaling of images,

Source: Internet
Author: User

C # proportional scaling of images,

// Proportional scaling of the Image public Image ZoomPicture (Image SourceImage, int TargetWidth, int TargetHeight) {int IntWidth; // The New Image Width int IntHeight; // New Image Height try {System. drawing. imaging. imageFormat format = SourceImage. rawFormat; System. drawing. bitmap SaveImage = new System. drawing. bitmap (TargetWidth, TargetHeight); Graphics g = Graphics. fromImage (SaveImage); g. clear (Color. white); // calculate the scaled image size http://www.cnblogs.com/roucheng/ If (SourceImage. width> TargetWidth & SourceImage. height <= TargetHeight) // The width of the target image is greater than that of the target image. The length is smaller than that of the target image. {IntWidth = TargetWidth; IntHeight = (IntWidth * SourceImage. height)/SourceImage. width;} else if (SourceImage. width <= TargetWidth & SourceImage. height> TargetHeight) // The width of the target image is smaller than that of the target image. The length is larger than that of the target image. {IntHeight = TargetHeight; IntWidth = (IntHeight * SourceImage. width)/SourceImage. height;} else if (SourceImage. width <= TargetWidth & SourceImage. height <= TargetHeight) // the aspect ratio. The length and width of the target image are both small {IntHeight = SourceImage. width; IntWidth = SourceImage. height;} else // the length and width of the target image are both large {IntWidth = TargetWidth; IntHeight = (IntWidth * SourceImage. height)/SourceImage. width; if (IntHeight> TargetHeight) // re-calculate {IntHeight = TargetHeight; IntWidth = (IntHeight * SourceImage. width)/SourceImage. height;} g. drawImage (SourceImage, (TargetWidth-IntWidth)/2, (TargetHeight-IntHeight)/2, IntWidth, IntHeight); SourceImage. dispose (); return SaveImage;} catch (Exception ex) {} return null ;}

 

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.