转载记录
<form id ="form1" action ="../handle/UploadFiles.ashx" method ="post" enctype ="multipart/form-data" style ="float: right;background-color:red" >
<input type ="file" name ="f1" value ="1231231" />
<input type ="submit" value ="123131222222" />
ashx:
public void ProcessRequest(HttpContext context)
{
context.Response .ContentType = "text/plain"
HttpPostedFile f1 = context.Request .Files ["f1" ]
String fileExt = System.IO .Path .GetExtension (f1.FileName )
System.Drawing .Image image = System.Drawing .Image .FromStream (f1.InputStream )
int newWidth = 300 , newHeight = 200
if ((decimal)image.Width / image.Height > (decimal)newWidth / newHeight)
{
newHeight = Convert.ToInt 32((decimal)image.Height * newWidth / image.Width )
}
else if ((decimal)image.Width / image.Height < (decimal)newWidth / newHeight)
{
newWidth = Convert.ToInt 32((decimal)image.Width * newHeight / image.Height )
}
System.Drawing .Bitmap bmp = new System.Drawing .Bitmap (newWidth, newHeight)
System.Drawing .Graphics g = System.Drawing .Graphics .FromImage (bmp)
g.SmoothingMode = System.Drawing .Drawing 2D.SmoothingMode .HighQuality
g.CompositingQuality = System.Drawing .Drawing 2D.CompositingQuality .HighQuality
g.InterpolationMode = System.Drawing .Drawing 2D.InterpolationMode .High
System.Drawing .Rectangle rectDestination = new System.Drawing .Rectangle (0 , 0 , newWidth, newHeight)
g.DrawImage (image, rectDestination, 0 , 0 , image.Width , image.Height , System.Drawing .GraphicsUnit .Pixel )
bmp.Save (context.Server .MapPath ("~/" ) + DateTime.Now .ToString ("yyyyMMddHHmmss" ) + fileExt)
bmp.Dispose ()
image.Dispose ()
context.Response .Write ("OK" )
}