#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 < ;/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