日期:2014-05-18 浏览次数:20484 次
//这是标题图片上传方法
if (image != null)
{
string year = DateTime.Now.Year.ToString();
string month = DateTime.Now.Month.ToString("00");
string day = DateTime.Now.Day.ToString("00");
string fileFolder = string.Concat("Upload/", year, "/", month, "/", day);
string path = HostingEnvironment.MapPath("~/" + fileFolder);
if (!Directory.Exists(path))
{
//在Upload目录下创建了一个文件夹
Directory.CreateDirectory(path);
}
string imagename = image.FileName;
string fileType = Path.GetExtension(image.FileName).ToLower();
if (fileType == ".jpeg" || fileType == ".jpg" || fileType == ".png" || fileType == ".gif")
{
Random ran = new Random();
string ename = DateTime.Now.ToString("yyyyMMddhhmmssfff") + ran.Next(9999);
//上传后生成的图片名
string name = ename + fileType;
image.SaveAs(Path.Combine(path, name));
string imageurl = string.Concat(HostingEnvironment.ApplicationVirtualPath, fileFolder, "/", name);
//生成标题图片
//缩略图名
string thumbName = ename + "_500x200" + fileType;
//缩略图绝对路径
string thumbPath = string.Concat(HostingEnvironment.ApplicationPhysicalPath, fileFolder, "/", thumbName);
// 设置缩略图保存路径
FileStream stream = new FileStream(Path.GetFullPath(thumbPath), FileMode.OpenOrCreate);
// 缩放上传的文件
Image OrigImage = Image.FromStream(image.InputStream);
// 创建缩略图对象
Bitmap TempBitmap = new Bitmap(550, 200);
// 创建缩略图画质
Graphics NewImage = Graphics.FromImage(TempBitmap);
NewImage.CompositingQuality = CompositingQuality.HighQuality;
NewImage.SmoothingMode = SmoothingMode.HighQuality;
NewImage.InterpolationMode = InterpolationMode.HighQualityBicubic;
// 创建Rectangle对象进行绘制
Rectangle imageRectangle = new Rectangle(0, 0, 550, 200);
NewImage.DrawImage(OrigImage, imageRectangle);
// 保存缩略图
TempBitmap.Save(stream, OrigImage.RawFormat);
// 释放资源
NewImage.Dispose();
TempBitmap.Dispose();
OrigImage.Dispose();
stream.Close();
stream.Dispose();
newArticle.ImageUrl = imageurl;
}
}
/// <summary>
/// 获取一个图片按等比例缩小后的大小。
/// </summary>
/// <param name="maxWidth">需要缩小到的宽度</param>
/// <param name="maxHeight">需要缩小到的高度</param>
/// <param name="imageOriginalWidt