C # proportional scaling of images,

Source: Internet
Author: User

C # proportional scaling of images,

Tool code:

Using System; using System. collections. generic; using System. drawing; using System. drawing. drawing2D; using System. drawing. imaging; using System. linq; using System. text; using System. threading. tasks; namespace ZoomImage. utils {/// <summary> /// image scaling /// </summary> public class ZoomImageUtil {/// <summary> /// image scaling /// </ summary> /// <param name = "bmp"> image </param> /// <param name = "width"> Target width, if the value is 0, the width is scaled proportionally </param> /// <param name = "height"> the target length. If the value is 0, indicates proportional length scaling </param> public static Bitmap GetThumbnail (Bitmap bmp, int width, int height) {if (width = 0) {width = height * bmp. width/bmp. height;} if (height = 0) {height = width * bmp. height/bmp. width;} Image imgSource = bmp; Bitmap outBmp = new Bitmap (width, height); Graphics g = Graphics. fromImage (outBmp); g. clear (Color. transparent); // you can specify the quality of the canvas. compositingQuality = CompositingQuality. highQuality; g. smoothingMode = SmoothingMode. highQuality; g. interpolationMode = InterpolationMode. highQualityBicubic; g. drawImage (imgSource, new Rectangle (0, 0, width, height + 1), 0, 0, imgSource. width, imgSource. height, GraphicsUnit. pixel); g. dispose (); imgSource. dispose (); bmp. dispose (); return outBmp ;}}}View Code

Example:

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. IO; using System. linq; using System. text; using System. threading; using System. threading. tasks; using System. windows. forms; using ZoomImage. utils; namespace ZoomImage {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void Form1_Load (object sende R, EventArgs e) {openFileDialog1.Multiselect = true;} private void txtWidth_KeyPress (object sender, KeyPressEventArgs e) {if (e. KeyChar! = 8 &&! Char. IsDigit (e. KeyChar) {e. Handled = true ;}} private void txtHeight_KeyPress (object sender, KeyPressEventArgs e) {if (e. KeyChar! = 8 &&! Char. isDigit (e. keyChar) {e. handled = true ;}} private void btnSelectImage_Click (object sender, EventArgs e) {try {if (txtWidth. text = "" & txtHeight. text = "") {return;} if (openFileDialog1.ShowDialog () = DialogResult. OK) {Task. factory. startNew () => {string path = Path. getDirectoryName (openFileDialog1.FileNames [0]) + "\ NewImage \"; int I = 0; foreach (string fileName in openFileDialo G1.FileNames) {Bitmap bmp = ZoomImageUtil. GetThumbnail (new Bitmap (fileName), Convert. ToInt32 (txtWidth. Text = ""? "0": txtWidth. Text), Convert. ToInt32 (txtHeight. Text = ""? "0": txtHeight. Text); if (! Directory. exists (path) {Directory. createDirectory (path);} File. delete (path + Path. getFileName (fileName); bmp. save (path + Path. getFileName (fileName); this. invoke (new InvokeDelegate () => {lblProgress. text = string. format ("Progress: {1}/{0}", openFileDialog1.FileNames. length, ++ I) ;})); Thread. sleep (1);} MessageBox. show ("successful! ") ;};}} Catch (Exception ex) {MessageBox. show (ex. message) ;}}/// <summary> /// delegate the cross-thread access control /// </summary> public delegate void InvokeDelegate ();}View Code

 

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.