Asp. Net uploads images and generates high-definition thumbnails

Source: Internet
Author: User
Tags bmp image

Asp. Net uploads images and generates high-definition thumbnails

Not very complex. Write it down. The purpose is only to achieve it, but not to follow the standards carefully. See the code that already exists on the Internet.

Using System. Drawing;

Page,

Click Submit:

The Code is as follows:  

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

The entire implementation process is shown in the figure below:

Browse the page and select an image:

After you click submit, the image and thumbnail are generated in the target Folder:

We can see that the text watermark in the thumbnail has been generated:

So far, the entire function has been implemented.

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.