Asp.net uploads images and generates thumbnails at the same time

Source: Internet
Author: User

Using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Drawing;
Public partial class slt_Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected void bt_upload_Click (object sender, EventArgs e)
{
// Check whether the format of the uploaded file is valid
If (this. UploadFile. PostedFile. ContentType. ToLower (). IndexOf ("image") <0)
{
Response. Write ("the format of the uploaded image is invalid! ");
Return;
}
// Generate the source Image
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; // source Image Width
Int oHeight = oImage. Height; // source Image Height
Int tWidth = 100; // you can specify the initial width of a thumbnail.
Int tHeight = 100; // you can specify the initial height of a thumbnail.
// Calculate the width and height of the thumbnail in proportion.
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 the thumbnail image
Bitmap tImage = new Bitmap (tWidth, tHeight );
Graphics g = Graphics. FromImage (tImage );
G. InterpolationMode = System. Drawing. Drawing2D. InterpolationMode. High; // set the High quality interpolation method.
G. SmoothingMode = System. Drawing. Drawing2D. SmoothingMode. HighQuality; // set high quality and smooth Low Speed
G. Clear (Color. Transparent); // Clear the canvas and fill it 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 (". ") +"/image/"+" o "+ DateTime. now. tow.datestring (). replace ("-", "") + DateTime. now. hour. toString () + DateTime. now. minute. toString () + DateTime. now. second. toString () + DateTime. now. millisecond. toString () + ". jpg "; // Save the physical path of the source Image
String tFullName = Server. mapPath (". ") +"/image/"+" t "+ DateTime. now. tow.datestring (). replace ("-", "") + DateTime. now. hour. toString () + DateTime. now. minute. toString () + DateTime. now. second. toString () + DateTime. now. millisecond. toString () + ". jpg "; // the physical path for saving the thumbnail
Try
{
// Save the image 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 resources
OImage. Dispose ();
G. Dispose ();
TImage. Dispose ();
}
}
 
}

From fengyarongaa

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.