C # Pictures Proportionally scaled sample code share

Source: Internet
Author: User
This article mainly for you in detail the C # picture scaled by the example, with a certain reference value, interested in small partners can refer to

The example of this article for everyone to share the C # picture proportionally scaled specific code for your reference, the specific content is as follows

Tool Class 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>///Picture scaling///</summary> public class Zoomimageutil {///<summary>//Picture scaling//</su mmary>//<param name= "bmp" > Pictures </param>///<param Name= "width" > target width, if 0, indicates the width is scaled proportionally </param >//<param name= "height" > target length, if 0, indicates proportional scaling of length </param> public static Bitmap GetThumbnail (Bitmap bmp, int w idth, 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);   Set the drawing quality of the canvas g.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; } }}

Examples of Use:

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 () {I  Nitializecomponent ();  } private void Form1_Load (object sender, 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 = = "" && txt    Height.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 Openfiledialog1.filenames) {Bitmap bmp = Zoomimageutil.getthumbnail (new Bitmap (fil ename), 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 ("Success!     ");    }); }} catch (Exception ex) {MessageBox.Show (ex).   Message); }}}///<summary>//delegate access to the control's delegates////</summary> public delegate void InvoKedelegate ();} 
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.