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 ();
}
}