ASP. NET upload image file automatically modify image size code

Source: Internet
Author: User

  #region Picture Scaling//<summary>//Picture scaling//</summary>//<param name= "Savepath" > Pictures Relative paths </param>///<param Name= "FileName" > Picture name </param>//<param name= "destwidth" > Zoom out &LT ;/param>//<param name= "destheight" > Height </param>//<param name= "type" >1--fixed scale; 2--proportionally scaled; 3 --Specifies the width, width greater than the specified width, equal to the specified width of the scale, less than the specified width of the original size of the upload; 4--artwork directly uploaded </param>///<returns></returns> public s tatic void Reducespic (string savepath,string fileName, int destwidth, int destheight, int type) {if (!f Ilename.equals ("")) {string allpath = System.Web.HttpContext.Current.Server.MapPath ("/") + Save                Path;                Generate original System.IO.Stream Stream = System.IO.File.OpenRead (Allpath + fileName);                System.Drawing.Image oimage = System.Drawing.Image.FromStream (stream); Stream.                Close (); Stream.    Dispose ();            System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort (THUMBNAILC                Allback);                String fileType = Filename.substring (Filename.lastindexof (".") + 1);                int owidth = Oimage.width;                int oheight = Oimage.height; int twidth = Destwidth; Set thumbnail initial width int theight = destheight;                     Sets the thumbnail initial height//By the specified width-height scaling if (type = = 1) {twidth = Destwidth;                Theight = Destheight;  }//proportionally calculates the width and height of the thumbnail if the else if (type = = 2) {if (Owidth > Twidth | |                            Oheight > Theight) {if (owidth >= oheight) { Theight = (int) Math.floor (convert.todouble (oheight) * (convert.todouble (twidth)/convert.todouble (o                        Width));               }         else {twidth = (int) Math.floor (convert.todouble (owidth) * (Con Vert.                        ToDouble (theight)/convert.todouble (oheight));                         }} else {twidth = Owidth;//Original width Theight = Oheight; Original height}}//Specify width, width greater than specified width, equal to the specified width, or less than the specified width to upload by original size else I F (Type = = 3) {if (owidth >= twidth) {if                        (Owidth >= oheight) {theight = (int) Math.floor (convert.todouble (oheight) * (convert.todouble (twidth)/Convert.tod                        Ouble (owidth)); } else {twidth = (int) Math.floor (convert.todoubl E (owidth) * (convert.todouble (theight)/convert.todouble (oheight)));                        }} else {Twidth = Owidth; Original width theight = oheight; Original Height}} else {twidth = Owidth;//Original width theight = oheight; Original height}//generate thumbnail artwork oimage = oimage.getthumbnailimage (Twidth, Theight, CALLB,                IntPtr.Zero);            Oimage.save (Allpath+filename);        }} public static bool Thumbnailcallback () {return false;} #endregion

  

ASP. NET upload image file automatically modify image size 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.