调用方法: UpLoadImage(file, "yasuo_", "20%", "20%");//返回的是压缩图
//<param name="sThumbExtension">缩略图的thumb</param>
//<param name="intThumbWidth">生成缩略图的宽度</param>
//<param name="intThumbHeight">生成缩略图的高度</param>
//<returns>缩略图名称</returns>
public string UpLoadImage(HttpPostedFileBase upImage, string sThumbExtension, string ThumbWidth, string ThumbHeight)
{
var intThumbWidth = 0;
var intThumbHeight = 0;
string sFilename = "";
var aa = "";
if (upImage != null)
{
//HttpPostedFile myFile = upImage.PostedFile;
int nFileLen = upImage.ContentLength;
if (nFileLen == 0)
return "请选择上传图片";
//获取upImage选择文件的扩展名
string extendName = System.IO.Path.GetExtension(upImage.FileName).ToLower();
//判断是否为图片格式
if (extendName != ".jpg" && extendName != ".jpge" && extendName != ".gif" && extendName != ".bmp" && extendName != ".png")
return "图片格式不正确";
byte[] myData = new Byte[nFileLen];
upImage.InputStream.Read(myData, 0, nFileLen);
sFilename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg"; //System.IO.Path.GetFileName(upImage.FileName); 修改图片名字
var dict = "/Uploads/Img/" + DateTime.Now.ToString("yyyyMM") + "/";
var dire = HttpContext.Server.MapPath("~") + dict;
//获得保存路径
//string filePath = Path.Combine(dire,upImage.FileName);
string filePath = Path.Combine(dire, sFilename);
if (!Directory.Exists(dire))
{
Directory.CreateDirectory(dire);//不存在就创建目录
}
upImage.SaveAs(filePath);
//以上为上传原图
try
{
//原图加载
using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(dict + sFilename)))
{
//原图宽度和高度
int width = sourceImage.Width;
int height = sourceImage.Height;
//等比缩放图片大小(按百分比)
decimal countHeight = decimal.Parse(ThumbHeight.Substring(0, ThumbHeight.IndexOf('%'))) / 100 * height;//计算百分比后的高度
decimal countWidth = decimal.Parse(ThumbWidth.Substring(0, ThumbWidth.IndexOf('%'))) / 100 * width;//计算百分比后的宽度
intThumbHeight = Convert.ToInt32(countHeight);
intThumbWidth = Convert.ToInt32(countWidth);
int smallWidth;
int smallHeight;
//获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽 和 原图的高/缩略图的高)
if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)
{
smallWidth = intThumbWidth;
smallHeight = intThumbWidth * height / width;
}
else
{
smallWidth = intThumbHeight * width / height;
smallHeight = intThumbHeight;
}
//缩略图保存的绝对路径
var yasou_dict = "/Uploads/Img_yasou/" + DateTime.Now.ToString("yyyyMM") + "/";
string yasouName = "yasou_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg";//"yasou_" + upImage.FileName;
aa = yasou_dict + yasouName;
var yasou_dire = HttpContext.Server.MapPath("~") + yasou_dict;
//获得保存路径
string yasou_filePath = Path.Combine(yasou_dire, yasouName);
if (!Directory.Exists(yasou_dire))
{
Directory.CreateDirectory(yasou_dire);//不存在就创建目录
}
//新建一个图板,以最小等比例压缩大小绘制原图
using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight))
{
//绘制中间图
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
{
//高清,平滑
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Black);
g.DrawImage(
sourceImage,
new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight),
new System.Drawing.Rectangle(0, 0, width, height),
System.Drawing.GraphicsUnit.Pixel
);
}
//新建一个图板,以缩略图大小绘制中间图
using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight))
{
//绘制缩略图 http://www.cnblogs.com/sosoft/
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1))
{
//高清,平滑
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Black);
int lwidth = (smallWidth - intThumbWidth) / 2;
int bheight = (smallHeight - intThumbHeight) / 2;
g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);
g.Dispose();
bitmap1.Save(yasou_filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
}
}
catch
{
//出错则删除(删除压缩图)
//System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath("Upload/aa/" + sFilename));
//删除原图
//System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath("Upload/" + sFilename));
return "图片格式不正确";
}
//返回缩略图路径名称
return aa;
}
return "请选择上传图片";
}