code example: uploading pictures in asp.net and generating thumbnails

Source: Internet
Author: User
Tags datetime tostring
asp.net| upload | upload pictures | thumbnail private void Btnuploadpicture_click (object sender, System.EventArgs e)
{
Check that the upload file is in a valid format
if (this. UploadFile.PostedFile.ContentType.ToLower (). IndexOf ("image") < 0)
{
Response.Write ("Upload picture format is invalid!") ");
Return
}

Generate artwork
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; Width of original artwork
int oheight = Oimage.height; Height of original artwork
int twidth = 100; Set the initial width of the thumbnail
int theight = 100; Set the initial height of the thumbnail

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)));
}
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 smooth degree
G.clear (color.transparent); Empty the canvas and fill with a transparent background color
G.drawimage (oimage,new Rectangle (0,0,twidth,theight), New Rectangle (0,0,owidth,oheight), graphicsunit.pixel);

String ofullname = Server.MapPath (".") + "/" + "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 of the original artwork
String tfullname = Server.MapPath (".") + "/" + "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 pictures 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
{
Releasing resources
Oimage.dispose ();
G.dispose ();
Timage.dispose ();
}
}

Related Article

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.