方法1
HttpFileCollection files = HttpContext.Current.Request.Files;
//我设置的大小不超过A4纸的缩略图
int newWidth=620;
int newHeight=880;
System.Drawing.Image img = null;
for(int iFile = 0; iFile < files.Count; iFile++)
{
///检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName);
string name=DateTime.Now.ToString("yyyyMMddHHmmssffffff")+iFile+fileExtension;
string filePath=System.Web.HttpContext.Current.Request.MapPath(@"UpdatePic/") + name;
if (fileExtension.ToLower()==".jpg" || fileExtension.ToLower()==".gif" || fileExtension.ToLower()==".png")
{
//图片大小限制在1K到2M之间
if(postedFile.ContentLength>=1024 && postedFile.ContentLength<=20480000 && postedFile.ContentType.ToString().ToLower().IndexOf("image") >= 0)
{
img = System.Drawing.Image.FromStream(postedFile.InputStream);
int iWidth = img.Width;
int iHeight = img.Height;
//如果宽和高都超过最大限制
if(iWidth > newWidth && iHeight > newHeight)
{
//判断下是宽更长还是高更长,以便以它做标准
if((double)iHeight/(double)iWidth > (double)newHeight/(double)newWidth)
{
//如果是高更长,设置高为最大限制值,宽等比例缩小
newWidth=(int)((double)iWidth/(double)iHeight * newHeight);
}
else
{
//如果是宽更长,设置宽为最大限制值,高等比例缩小
newHeight=(int)((double)iHeight/(double)iWidth * newWidth);
}
}
else if(iWidth > newWidth && iHeight <= newHeight)
{
//如果只是宽超过限制,设置宽为最大值,高等比例缩小
newHeight=(int)((double)iHeight/(double)iWidth * newWidth);
}
else if(iWidth <= newWidth && iHeight > newHeight)
{
//如果只是高超过限制,设置高为最大值,宽等比例缩小
newWidth=(int)((double)iWidth/(double)iHeight * newHeight);
}
else
{
//如果都没超过现在用原来的大小
newWidth=iWidth;
newHeight=iHeight;
}
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(width,height);
//新建一个画板
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(Color.Transparent);
//按指定大小绘制原图片
g.DrawImage(img, new Rectangle(0, 0, width, height));
try
{
//以jpg格式保存缩略图,图片质量更高
bitmap.Save(imgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch(System.Exception e)
{
throw e;
}
finally
{
img.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
}
}
}
方法二。
private void btnUploadPicture_Click(object sender, System.EventArgs e)
{
//检查上传文件的格式是否有效
if(this.UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0)
{
Response.Write("上传图片格式无效!");
return;
}
//生成原图
Byte[] oFileByte = new byte[this.UploadFile.PostedFile.ContentLength];
System.IO.Stream oStream = this.UploadFile.PostedFile.InputStream;
System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
int oWidth = oImage.Width; //原图宽度
int oHeight = oImage.Height; //原图高度
int tWidth = 100; //设置缩略图初始宽度
int tHeight = 100; //设置缩略图初始高度
//按比例计算出缩略图的宽度和高度
if(oWidth >= oHeight)
{
tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
}
else
{
tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
}
//生成缩略原图
Bitmap tImage = new Bitmap(tWidth,tHeight);
Graphics g = Graphics.FromImage(tImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
g.Clear(Color.Transparent); //清空画布并以透明背景色填充
g.DrawImage(oImage,new Rectangle(0,0,tWidth,tHeight),new Rectangle(0,0,oWidth,oHeight),GraphicsUnit.Pixel);
string oFullName = Server.MapPath(".") "/" "o" DateTime.Now.ToShortDateString().Replace("-","") DateTime.Now.Hour.ToString() DateTime.Now.Minute.ToString() DateTime.Now.Second.ToString() DateTime.Now.Millisecond.ToString() ".jpg"; //保存原图的物理路径
string tFullName = Server.MapPath(".") "/" "t" DateTime.Now.ToShortDateString().Replace("-","") DateTime.Now.Hour.ToString() DateTime.Now.Minute.ToString() DateTime.Now.Second.ToString() DateTime.Now.Millisecond.ToString() ".jpg"; //保存缩略图的物理路径
try
{
//以JPG格式保存图片
oImage.Save(oFullName,System.Drawing.Imaging.ImageFormat.Jpeg);
tImage.Save(tFullName,System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch(Exception ex)
{
throw ex;
}
finally
{
//释放资源
oImage.Dispose();
g.Dispose();
tImage.Dispose();
}
}