asp.net upload pictures and create high-definition thumbnails

Source: Internet
Author: User
Tags datetime trim

Not very complicated, probably write it. The goal is to achieve, not carefully according to the standard or what to write. This refers to the code that already exists on the Internet.

Using System.Drawing;

page, as shown in figure:

Click the Submit button:

The code is as follows

Httppostedfile HPF = uploadimage.postedfile;
//Get filename (without path)
String Filename = Path.getfilename (HPF. FileName);/The original text modifies the
if (HPF). Filename.length < 1
{
Response.Write ("Please select the picture file you want to upload");
return;
}
if (HPF. ContentType!= "Image/jpeg" && hpf. ContentType!= "image/gif")//Original modification
{
Response.Write ("Only allow uploading of gif jpg files");
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 picture to the server
Try
{
Hpf. SaveAs (Server.MapPath ("Album") + Filename);//Modify yourself!
}
catch (Exception ee)
{
Response.Write ("Upload picture failed, reason" + EE.) message);
Return
}

Generate thumbnails
Original picture name
String originalfilename = HPF. FileName;
High-quality picture name generated
String strfile = Server.MapPath ("Albumsmall_") + Filename;

Get a Picture 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); Picture size
System.Drawing.Image bitmap = new System.Drawing.Bitmap (size. Width, size. Height); Create a new BMP picture
System.Drawing.Graphics Graphics = System.Drawing.Graphics.FromImage (bitmap); New artboard
Graphics. Interpolationmode = System.Drawing.Drawing2D.InterpolationMode.High; Set high quality interpolation method
Graphics. SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; Set high quality, low speed rendering smooth degree
Graphics. Clear (Color.White); Empty canvas
Paint at a specified location
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 ("Song Body", 10);
System.Drawing.Brush Brush = new SolidBrush (color.black);
Textgraphics.drawstring (TextBox3.Text.Trim (), font, brush, 10, 10);
Textgraphics.dispose ();


Picture watermark
System.Drawing.Image copyimage = System.Drawing.Image.FromFile (System.Web.HttpContext.Current.Server.MapPath (" Pic/1.gif "));
Graphics a = graphics.fromimage (bitmap);
A.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 thumbnails
Try
{
Bitmap. Save (strfile, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
Response.Write ("Save thumbnail failed:" + ex.) message);
}

Graphics. Dispose ();
Image. Dispose ();
Bitmap. Dispose ();

The entire implementation process is shown in the following diagram:

Browse page, select Picture:

After clicking Submit, pictures and thumbnails have been generated to the target folder:

You can see that the text watermark inside the thumbnail has been generated:

At this point, the entire feature 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.