The example in this article describes how asp.net implements uploading pictures and generating thumbnails. Share to everyone for your reference, specific as follows:
protected void Bt_upload_click (object sender, EventArgs e) {//check that the format of the uploaded file is valid if (this. UploadFile.PostedFile.ContentType.ToLower (). IndexOf ("image") < 0) {Response.Write ("Upload picture format is not valid!")
");
Return }//Generate original byte[] Ofilebyte = new Byte[this.
UploadFile.PostedFile.ContentLength]; System.IO.Stream ostream = this.
UploadFile.PostedFile.InputStream;
System.Drawing.Image oimage = System.Drawing.Image.FromStream (ostream); int owidth = Oimage.width; Original artwork width int oheight = oimage.height; Original height int twidth = 100; Set the initial width of the thumbnail int theight = 100; Set thumbnail initial height//proportionally calculate the width and height of the thumbnail if (owidth >= oheight) {theight = (int) Math.floor (convert.todouble (oheight) *
(Convert.todouble (twidth)/convert.todouble (owidth)); else {twidth = (int) Math.floor (convert.todouble (owidth) * (convert.todouble (theight)/convert.todouble (OHeight)
));
///Generate thumbnail artwork Bitmap timage = new Bitmap (twidth, theight);
Graphics g = graphics.fromimage (timage); G.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.High; Set high quality interpolation method G.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;//Set high quality, low speed rendering smoothness degree g.clear ( Color.transparent); Empty the canvas and fill the g.drawimage with a transparent background color (oimage, new Rectangle (0, 0, Twidth, theight), new Rectangle (0, 0, Owidth, oheight), Graphic
Sunit.pixel); String ofullname = Server.MapPath (".") + "/image/" + "O" + DateTime.Now.ToShortDateString (). Replace ("-", "") + DateTime.Now.Hour.ToString () + DateTime.Now.Minute.ToString () + DateTime.Now.Second.ToString () + DateTime.Now.Millisecond.ToString () + ". jpg"; Save the physical path to the original image string tfullname = Server.MapPath (".") + "/image/" + "T" + DateTime.Now.ToShortDateString (). Replace ("-", "") + DateTime.Now.Hour.ToString () + DateTime.Now.Minute.ToString () + DateTime.Now.Second.ToString () + DateTime.Now.Millisecond.ToString () + ". jpg";
Save the physical path of the thumbnail try {//Save the picture in JPG format oimage.save (ofullname, System.Drawing.Imaging.ImageFormat.Jpeg); Timage.save (Tfullname, System.Drawing.ImaGing.
IMAGEFORMAT.JPEG);
catch (Exception ex) {throw ex;
finally {//release resource oimage.dispose ();
G.dispose ();
Timage.dispose ();
}
}
}
Here's another way to improve:
#region Upload a picture and generate thumbnails///<summary>///upload images generate thumbnails///</summary>///<param name= "Originalimagepath" > Photo Source Road Diameter </param>///<param name= "Thumbnailpath" > Thumbnail path (physical path) </param>///<param name= "width" > thumbnail width </param>///<param name= "height" > Thumbnail height </param>///<param name= "mode" > How to Generate Thumbnails </param > public static void Makethumbnail (String originalimagepath, string thumbnailpath, int width, int height, string mode)
{//Get the source picture from the path file System.Drawing.Image originalimage = System.Drawing.Image.FromFile (Originalimagepath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0; Gets the width of the picture int ow = image.
Width; Gets the height of the picture int oh = image.
Height;
How to generate thumbnails switch (mode) {case ' HW ': break;
Case "W"://specified width high proportionally toheight = Originalimage.height * width/originalimage.width;
Break
Case "H"://The height of the specified picture is proportional towidth = originalimage.width * height/originalimage.height;
Break Case "cut"://If the reduction mode is not deformed if (double) originalimage.width/(double) originalimage.height > (double) towidth/(Do
uble) toheight) {oh = Originalimage.height;
The width of the thumbnail picture ow = originalimage.height * towidth/toheight;
y = 0;
x = (Originalimage.width-ow)/2;
else {ow = Originalimage.width;
The height of the thumbnail picture Oh = originalimage.width * toheight/towidth;
x = 0;
Y (Originalimage.height-oh)/2;
} break;
Default:break;
//Create a new bmp picture Bitmap Bitmap = new Bitmap (towidth, toheight);
Create a new canvas to bitmap width as the size of the canvas Graphics g = graphics.fromimage (bitmap);
Setting high quality interpolation method g.interpolationmode = Interpolationmode.high;
With high quality low speed present G.smoothingmode = smoothingmode.highquality;
Empty the canvas to fill the g.clear (color.transparent) with a white background color; Draws the specified portion of the original picture at the specified location g.drawimage (originalimage,new Rectangle (towidth,toheight), New Rectangle (X,y,ow,oh),
GraphicsUnit.Pixel); try {//save thumbnail bitmap in jpg format. Save (tHUMBNAILPATH,SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.JPEG);
catch (Exception ex) {throw ex;
finally {//release resource originalimage.dispose (); Bitmap.
Dispose ();
G.dispose ();
}} #endregion
I hope this article will help you with ASP.net programming.