Upload images and generate thumbnails in ASP. NET

Source: Internet
Author: User

Upload images and generate thumbnails in ASP. NET. You can refer to the following code:

Private void btnUploadPicture_Click (object sender, System. 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 a 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 (".") + "/" + "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 (".") + "/" + "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 ();

}

}

 

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.