1. Add an HTML
<! DOCTYPE html>"http://www.w3.org/1999/xhtml">"Content-type"Content="text/html; Charset=utf-8"/> <title></title>"getmin.ashx"Method="Post"Enctype="Multipart/form-data">Photo:<input type="file"Name="file"Id="file"Onchange="javascript:fileinfo ();"/> <p style="font-size:12px;"> Original width: <label id="ow">0</label> Original height: <label id="Oh">0</label> Original size:<label id="OS">0</label>KB</p> <p> Set width: <input type="text"Name="width"style="width:50px;"/> <span style="color: #09a02c; font-size:12px"> Height by original ratio </span></p> <p><input type="Submit"style="Color: #ff6a00"Value="Download thumbnail image"/> </p> </form> </body>"Text/javascript">//get picture information before uploading (file with HTML5)function FileInfo () {varf = document.getElementById ("file"). files[0]; varReader =NewFileReader (); Reader.readasdataurl (f); Reader.onload=function (e) {vardata =E.target.result; //load a picture to get the width height varImage =NewImage (); IMAGE.SRC=data; Image.onload=function () {varwidth =Image.width; varHeight =Image.height; varSize = parseint (F.size/1024x768); document.getElementById ("ow"). InnerHTML =width; document.getElementById ("Oh"). InnerHTML =height; document.getElementById ("OS"). InnerHTML =size; }; } } </script>
2. Add a handler: ashx
Public voidProcessRequest (HttpContext context) {//1. Get user-uploaded file streamsHttppostedfile file = context. request.files[0]; //Get file name stringFileName =file. FileName; //Get extension stringExtension =path.getextension (fileName). ToUpper (); //2. Create a picture based on the file stream uploaded by the user using(Image originalimage =image.fromstream (file. InputStream)) {//get the width and height of the original picture intOwidth =Originalimage.width; intOheight =Originalimage.height; //the width of the thumbnail image intMwidth = Convert.ToInt32 (context. request.form["width"]); //equal proportions of high, take integers intMheight = Mwidth * Oheight/Owidth; //3. Create a scaled-down picture based on the original picture, equal proportions using(Image minimage =NewBitmap (Mwidth, Mheight)) { //4. Draw the big picture to the small picture//Create a Canvas object based on a small imageGraphics gmin =graphics.fromimage (minimage); //put the big picture on the small pictureGmin. DrawImage (Originalimage,0,0, Mwidth, mheight); //5. Download thumbnail imageMemoryStream ms =NewMemoryStream (); //determine the type of pictureImageFormat imageformat =NULL; stringContentType =""; Switch(Extension) { Case ". JPG": ImageFormat = imageformat.jpeg; ContentType ="Image/jpeg"; Break; Case ". PNG": ImageFormat = imageformat.png; ContentType ="Image/png"; Break; Case ". GIF": ImageFormat = imageformat.gif; ContentType ="Image/gif"; Break; //................ If you need another picture format to continue adding} minimage.save (Ms,imageformat); Context. Response.clearcontent (); Context. Response.AddHeader ("content-disposition","attachment; Filename="+Httputility.urlencode (FileName, System.Text.Encoding.UTF8)); Context. Response.ContentType=ContentType; Context. Response.BinaryWrite (Ms. ToArray ()); Context. Response.End (); } } } Public BOOLisreusable {Get { return false; } }
NET Generate thumbnails