1 #regionUpload a picture to generate a thumbnail image2 /// <summary>3 ///Upload Image4 /// </summary>5 /// <param name= "Sender" ></param>6 /// <param name= "E" ></param>7 Public voidBtnupload_click (Objectsender, EventArgs e)8 {9 //Check that the format of the uploaded file is validTen if( This. FudPhoto.PostedFile.ContentType.ToLower (). IndexOf ("Image") <0) One { AResponse.Write ("<script>alert (' Upload image format invalid! '); window.location.href= ' les_applytopactivityadd.aspx ';</script>"); - return; - } the stringFilenmae; - stringFileNameS; - stringname = FudPhoto.PostedFile.FileName;//Client file path -FileInfo file =NewFileInfo (name); + stringFileName =file. Name; - stringExtname =Getfileext (fileName). ToUpper (); + //Generate artwork Abyte[] Ofilebyte =New byte[ This. FudPhoto.PostedFile.ContentLength]; atSystem.IO.Stream OStream = This. FudPhoto.PostedFile.InputStream; -System.Drawing.Image Oimage =System.Drawing.Image.FromStream (oStream); - intTowidth = $;//set thumbnail initial height - intToheight = $;//set thumbnail initial width - intx =0; - inty =0; in intow =Oimage.width; - intOh =Oimage.height; to + if(ow = =640&& ow = =640) - { the //calculates the width and height of the thumbnail proportionally * $ if(((Double) Oimage.width/(Double) oimage.height) > ((Double) Towidth/(Double) toheight))Panax Notoginseng { -Oh =Oimage.height; theow = Oimage.height * Towidth/Toheight; +y =0; Ax = (Oimage.width-ow)/2; the } + Else - { $ow =Oimage.width; $Oh = oimage.width * Toheight/Towidth; -x =0; -y = (Oimage.height-oh)/2; the } - //Generate thumbnail artworkWuyiBitmap Timage =NewBitmap (Towidth, toheight); theGraphics g =graphics.fromimage (timage); -G.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.High;//setting high-quality interpolation methods WuG.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//set high quality, low speed rendering smoothness -G.clear (color.transparent);//Empty the canvas and fill it with a transparent background color AboutG.drawimage (Oimage,NewRectangle (0,0, Towidth, Toheight),NewRectangle (0,0, Ow, OH), graphicsunit.pixel); $ -Filenmae = DateTime.Now.ToShortDateString (). Replace ("-","") + DateTime.Now.Hour.ToString () + DateTime.Now.Minute.ToString () + DateTime.Now.Second.ToString () + DateTime.Now.Millisecond.ToString () +"."+ Extname;//Save the physical path to the original - stringOfullname = Server.MapPath ("/uploadfiles//"+filenmae); - AFileNameS = Filenmae +"- S"+". JPG";//Save the physical path of the thumbnail image + stringTfullname = Server.MapPath ("/uploadfiles//"+FileNameS); the Try - { $ //save a picture in JPG format the Oimage.save (Ofullname, System.Drawing.Imaging.ImageFormat.Jpeg); the Timage.save (Tfullname, System.Drawing.Imaging.ImageFormat.Jpeg); the the //Lbmessage.text = "Hint: File" "+ Filenmae +" "Upload successfully and generate" "+ FileNameS +" "thumbnail, File type:" + - //FudPhoto.PostedFile.ContentType + ", File size:" + fudPhoto.PostedFile.ContentLength + "B"; inLbmessage.text ="Tip: The file was successfully uploaded and generated ""+ FileNameS +""Thumbnail!" "; the stringPIC =Filenmae; theviewstate["pic"] =PIC; About } the Catch(Exception ex) the { the Throwex; + } - finally the {Bayi //Freeing Resources the oimage.dispose (); the g.dispose (); - timage.dispose (); - } the } the Else the { theLbmessage.text ="tip: The size of the picture should be 640x640"; - } the } the Private stringGetfileext (stringfileName) the {94 if(Filename.indexof (".") == -1) the return ""; the string[] temp = Filename.split ('.'); the returnTemp[temp. Length-1]. ToLower ();98 } About #endregion -}
:
C # upload images to generate thumbnails, control image upload size.