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