Asp.net Image Upload and thumbnail generation

Source: Internet
Author: User
Using system; using system. data; using system. configuration; using system. web; using system. web. security; using system. web. ui; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. web. UI. htmlcontrols; using system. io; using system. text; using system. drawing; /// <summary> /// Image Upload class // </Summary> public class uploadimg {public uploadimg () {}/ * call: after instantiating the class, set the desired attribute. Uploadimg (this. fileupload1) input the ID of the file upload control as the parameter. */Private Static string _ MSG; private string ofullname; // The complete path for saving. original Image private string tfullname; // The complete path for saving. thumbnail Private Static string _ ofilename; // The Name Of The saved image. original Image private string tfilename; // The image name when it is saved. thumbnail // Private string _ tfullname = "0"; // Private string _ ofilename = "0"; // Private string _ tfilename = "0 "; private string _ tprefix = "small _"; private int _ limitwidth = 3072; private int _ limitheight = 2304; private int _ twidth = 100; private int _ theight = 100; private int _ size = 3000000; private bool _ israte; private bool _ iscreate; private string _ Path = "Upload"; // <summary> // information // </Summary> Public static string MSG {get {return _ MSG ;} set {_ MSG = value ;}/// <summary> /// thumbnail prefix /// </Summary> Public String tprefix {get {return _ tprefix ;} set {_ tprefix = value ;}/// <s Ummary >/// maximum width of the source image /// </Summary> Public int limitwidth {get {return _ limitwidth;} set {_ limitwidth = value ;}} /// <summary> /// maximum height of the source image /// </Summary> Public int limitheight {get {return _ limitheight;} set {_ limitheight = value ;}} /// <summary> /// maximum width of the thumbnail /// </Summary> Public int twidth {get {return _ twidth;} set {_ twidth = value ;}} /// <summary> /// maximum height of the thumbnail /// </Summary> Publ IC int theight {get {return _ theight;} set {_ theight = value ;}} /// <summary> /// file size /// </Summary> Public int size {get {return _ size;} set {_ size = value ;}} /// <summary> /// proportional to/// </Summary> Public bool israte {get {return _ israte;} set {_ israte = value ;}} /// <summary> /// whether to generate a thumbnail // </Summary> Public bool iscreate {get {return _ iscreate;} set {_ iscreate = value ;}} /// <Summary> /// name of the folder where the image is stored /// </Summary> Public String path {get {return _ path ;} set {_ Path = value ;}/// <summary> // source image name /// </Summary> Public static string ofilename {get {return _ ofilename ;} set {_ ofilename = value ;}/// <summary> // Image Upload (default: "proportional compression, limited upload size: 2048*1536, thumbnail size: 100*100, the upload size is limited to 1 MB and is stored in the root directory upload ") /// </Summary> /// <Param name = "uploadfile"> File Upload Control </param> /// <return S> whether the image is successfully saved </returns> Public bool uploadimg (fileupload uploadfile) {If (uploadfile. hasfile) // check whether the selected file {string filename = uploadfile. filename. tolower (); int I = filename. lastindexof (". "); filename = filename. substring (I, filename. length-I); If (! (Filename = ". jpg "| filename = ". JPEG "| filename = ". GIF "| filename = ". PNG "| filename = ". BMP ") {MSG =" the file format is incorrect. It can only be JPEG, JPG, GIF, PNG or BMP! "; Return false;} // check whether the format of the uploaded file is valid if (uploadfile. postedfile. contentlength = 0 | uploadfile. postedfile. contentlength> = size) {MSG = string. format ("the image size should be smaller than {0} m! ", Math. round (_ size/1024.0/1024.0, 2); Return false;} // check the image file size // generate the source image stream ostream = uploadfile. postedfile. inputstream; system. drawing. image oimage = system. drawing. image. fromstream (ostream); int owidth = oimage. width; // source Image Width int oheight = oimage. height; // source Image Height if (owidth> limitwidth | oheight> limitheight) {MSG = string. format ("image size should be less than {0} * {1}", _ limitwidth, this. _ limitheight); Return false ;} // Check whether the size exceeds the specified size if (israte) {// calculate the width and height of the thumbnail proportionally if (owidth> = oheight) {theight = (INT) math. floor (convert. todouble (oheight) * (convert. todouble (twidth)/convert. todouble (owidth); // equals set height} else {twidth = (INT) math. floor (convert. todouble (owidth) * (convert. todouble (theight)/convert. todouble (oheight); // proportional width} // generate the thumbnail image bitmap timage = new Bitmap (twidth, theight); graphics G = graphics. fromi MAGE (timage); G. interpolationmode = system. drawing. drawing2d. interpolationmode. highqualitybicubic; // set the high quality interpolation method G. smoothingmode = system. drawing. drawing2d. smoothingmode. highquality; // set high quality, low speed rendering smoothness G. clear (color. transparent); // clear the canvas and fill g with a transparent background color. drawimage (oimage, new rectangle (0, 0, twidth, theight), new rectangle (0, 0, owidth, oheight), graphicsunit. pixel); random orandom = new random (); string ostr Ingrandom = orandom. next (1, 999999999 ). tostring (); // generate nine random numbers // format the date as the file name string ostringtime = string. format ("{0: yyyymmddhhmmss}", datetime. now); ofilename = ostringtime + ostringrandom + filename; tfilename = tprefix + ostringtime + ostringrandom + filename; string savepath = httpcontext. current. server. mappath ("~ ") +" \ "+ Path +" \ "; if (! Directory. exists (savepath) {directory. createdirectory (savepath); // create a folder under the root directory} ofullname = savepath + ofilename; tfullname = savepath + tfilename; // start to save the image to the server try {Switch (filename) {Case ". JPEG ": Case ". jpg ": {oimage. save (ofullname, system. drawing. imaging. imageformat. JPEG); If (iscreate) timage. save (tfullname, system. drawing. imaging. imageformat. JPEG); break;} case ". GIF ": {oimage. save (ofulln Ame, system. drawing. imaging. imageformat. GIF); If (iscreate) timage. save (tfullname, system. drawing. imaging. imageformat. GIF); break;} case ". PNG ": {oimage. save (ofullname, system. drawing. imaging. imageformat. PNG); If (iscreate) timage. save (tfullname, system. drawing. imaging. imageformat. PNG); break;} case ". BMP ": {oimage. save (ofullname, system. drawing. imaging. imageformat. BMP); If (iscreate) timage. SA Ve (tfullname, system. Drawing. imaging. imageformat. BMP); break ;}} MSG = "image uploaded successfully! "; // Save path + complete file name string _ savepath ="/"+ path +"/"; ofullname = _ savepath + ofilename; tfullname = _ savepath + tfilename; return true;} catch (exception ex) {MSG = ex. message; return false;} finally {// release resource oimage. dispose (); G. dispose (); timage. dispose () ;}} else {MSG = "select the image to upload first! "; Return false ;}}}

 

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.