|
HttpPostedFile hpf = UploadImage. PostedFile;
// Get the file name (excluding the path)
String Filename = Path. GetFileName (hpf. FileName); // original modification
If (hpf. FileName. Length <1)
{
Response. Write ("select the image file you want to upload ");
Return;
}
If (hpf. ContentType! = "Image/jpeg" & hpf. ContentType! = "Image/gif") // original modification
{
Response. Write ("only files of the gif jpg type can be uploaded ");
Return;
}
Else
{
System. Text. StringBuilder sb = new System. Text. StringBuilder ();
Sb. Append (DateTime. Now. Year. ToString ());
Sb. Append (DateTime. Now. Month. ToString ());
Sb. Append (DateTime. Now. Day. ToString ());
Sb. Append (DateTime. Now. Hour. ToString ());
Sb. Append (DateTime. Now. Minute. ToString ());
Sb. Append (DateTime. Now. Second. ToString ());
If (Filename. ToLower (). EndsWith ("gif "))
{
Sb. Append (". gif ");
}
Else if (Filename. ToLower (). EndsWith ("jpg "))
{
Sb. Append (". jpg ");
}
Else if (Filename. ToLower (). EndsWith ("jpeg "))
{
Sb. Append (". jpeg ");
}
Filename = sb. ToString ();
}
// Save the image to the server
Try
{
Hpf. SaveAs (Server. MapPath ("Album \") + Filename); // modify it by yourself!
}
Catch (Exception ee)
{
Response. Write ("image Upload failed, cause" + ee. Message );
Return;
}
// Generate a thumbnail
// Original image name
String originalFilename = hpf. FileName;
// Generated high-quality image name
String strFile = Server. MapPath ("Album \ Small _") + Filename;
// Obtain an image object from a file
System. Drawing. Image image = System. Drawing. Image. FromStream (hpf. InputStream, true );
Double Width = Double. Parse (TextBox1.Text. Trim ());
Double Height = Double. Parse (TextBox2.Text. Trim ());
System. Double NewWidth, NewHeight;
If (image. Width> image. Height)
{
NewWidth = Width;
NewHeight = image. Height * (NewWidth/image. Width );
}
Else
{
NewHeight = Height;
NewWidth = (NewHeight/image. Height) * image. Width;
}
If (NewWidth> Width)
{
NewWidth = Width;
}
If (NewHeight> Height)
{
NewHeight = Height;
}
System. Drawing. Size size = new Size (int) NewWidth, (int) NewHeight); // Image Size
System. Drawing. Image bitmap = new System. Drawing. Bitmap (size. Width, size. Height); // Create a bmp Image
System. Drawing. Graphics graphics = System. Drawing. Graphics. FromImage (bitmap); // create a Drawing board
Graphics. InterpolationMode = System. Drawing. Drawing2D. InterpolationMode. High; // set the High quality interpolation method.
Graphics. SmoothingMode = System. Drawing. Drawing2D. SmoothingMode. HighQuality; // set high quality and smooth low speed
Graphics. Clear (Color. White); // Clear the canvas
// Draw a picture at a specified position
Graphics. DrawImage (image, new System. Drawing. Rectangle (0, 0, bitmap. Width, bitmap. Height ),
New System. Drawing. Rectangle (0, 0, image. Width, image. Height ),
System. Drawing. GraphicsUnit. Pixel );
// Text watermark
System. Drawing. Graphics textGraphics = System. Drawing. Graphics. FromImage (bitmap );
System. Drawing. Font font = new Font ("", 10 );
System. Drawing. Brush brush = new SolidBrush (Color. Black );
TextGraphics. DrawString (TextBox3.Text. Trim (), font, brush, 10, 10 );
TextGraphics. Dispose ();
/// Image watermark
// System. Drawing. Image copyImage = System. Drawing. Image. FromFile (System. Web. HttpContext. Current. Server. MapPath ("pic/1.gif "));
// Graphics a = Graphics. FromImage (bitmap );
//. DrawImage (copyImage, new Rectangle (bitmap. width-copyImage.Width, bitmap. height-copyImage.Height, copyImage. width, copyImage. height), 0, 0, copyImage. width, copyImage. height, GraphicsUnit. pixel );
// CopyImage. Dispose ();
// A. Dispose ();
// CopyImage. Dispose ();
// Save the thumbnail
Try
{
Bitmap. Save (strFile, System. Drawing. Imaging. ImageFormat. Jpeg );
}
Catch (Exception ex)
{
Response. Write ("failed to save the thumbnail:" + ex. Message );
}
Graphics. Dispose ();
Image. Dispose ();
Bitmap. Dispose (); |