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