This article transferred from: http://www.cnblogs.com/promic/archive/2010/04/21/1717190.html
Private Static stringStrConnect = system.configuration.configurationmanager.appsettings["ConnStr"]; //Uploading Images protected voidBtnupload_click (Objectsender, EventArgs e) {...stringImgtype; stringFullfilename = This. TxtPath.PostedFile.FileName; Imgtype= Fullfilename.substring (Fullfilename.lastindexof (".") +1). ToLower (); stringUserdirectory = session["UserID"]. ToString ();//User ID is the name of the folder you created stringUserPath = Server.MapPath ("UpLoad"). ToString () +"\\"+userdirectory; //If the folder does not exist, create if(!directory.exists (UserPath)) {directory.createdirectory (UserPath); } stringOriginalimagepath = UserPath +"\\"+"Original"; if(!directory.exists (Originalimagepath)) {directory.createdirectory (Originalimagepath); } stringThumbnailpath = UserPath +"\\"+"Thumbnail"; if(!directory.exists (Thumbnailpath)) {directory.createdirectory (Thumbnailpath); } //get the file name of the uploaded image stringFileName =txtName.Text; ...if(imgtype=="jpg"|| imgtype=="JPEG"|| imgtype=="BMP"|| imgtype=="PNG"|| imgtype=="icon") { Try { //Generate artwork byte[] Ofilebyte =New byte[ This. TxtPath.PostedFile.ContentLength]; Stream OStream= This. TxtPath.PostedFile.InputStream; System.Drawing.Image Oimage=System.Drawing.Image.FromStream (OStream); intOwidth = Oimage.width;//Original Width intOheight = Oimage.height;//Original Height intTwidth = -;//set thumbnail initial width intTheight = -;//set thumbnail initial width//calculates the width and height of the thumbnail proportionally if(Owidth >=oheight) {Theight= (int) Math.floor (convert.todouble (oheight) * (convert.todouble (twidth)/convert.todouble (oheight)); } Else{twidth= (int) Math.floor (convert.todouble (owidth) * (convert.todouble (theight)/convert.todouble (oheight)); } //generate thumbnail imagesBitmap Timage =NewBitmap (twidth,theight); Graphics g=graphics.fromimage (timage); G.interpolationmode= System.Drawing.Drawing2D.InterpolationMode.High;//setting high-quality interpolation methodsG.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//set high quality, low speed rendering smoothnessG.clear (color.transparent);//Empty the canvas and fill it with a transparent background colorG.drawimage (Oimage,NewRectangle (0,0, Twidth, Theight),NewRectangle (0,0, Owidth, oheight), GraphicsUnit.Pixel); //This.txtPath.PostedFile.SaveAs (Originalimagepath + "\" + FileName + "." + Imgtype); //string originalimageurl = "upload/" + userdirectory + "/" + "orginal/" +filename + ". jpg";//get the virtual path of the original image on the server//string thumbnailimageurl = "upload/" + userdirectory + "/" + "thumbnail/" + fileName + ". jpg";//get the virtual path of the original image on the server//save images in jpg format//Oimage.save (Originalimageurl, System.Drawing.Imaging.ImageFormat.Jpeg);/* See if there's a problem with these two ways of saving * *//Timage.save (Thumbnailimageurl, System.Drawing.Imaging.ImageFormat.Jpeg); stringOPath = Originalimagepath +"\\"+ FileName +". jpg"; stringTpath = Thumbnailpath +"\\"+ FileName +". jpg"; //save images in jpg formatOimage.save (OPath, System.Drawing.Imaging.ImageFormat.Jpeg); Timage.save (Tpath, System.Drawing.Imaging.ImageFormat.Jpeg); Lblmessage.visible=true; Lblmessage.text="Image upload Successful! "; txtName.Text=""; Txtdescription.text=""; //Freeing ResourcesOimage.dispose (); G.dispose (); Timage.dispose (); } Catch(Exception ex) {lblmessage.visible=true; Lblmessage.text="upload file error due to network reason"+Ex. Message; } } Else{Response.Write ("<script language= ' javascript ' >alert (' You choose the wrong image format! ');</script>"); Lblmessage.visible=false; }}