贴段上传图片代码,截取缩略图

[PS:原创,如转载,请注明转载出处,谢谢]

这段代码杂用?有啥用?能干什么?

最近在做一个图片网,你说能干嘛用呢??呵呵。。。

一张高清图,最少都是一MB以上的,看了这个之后,你还在用原来的,只保存一张图片???

那你显示列表页的时候,不卡死了才怪了,一个页面显示60张图片,一张图片来个不多,1MB,整个列表下来是60MB。。。老大,强悍。。。牛B哇。。。嘿嘿

搞啥?不搞啥,切出来后,在搞,显示缩略图,弄成几十K的大小,显示一张比例为800*600的图,用到详细页面去,在搞个原图,用来点击查看原图用,这样就不用当心了吧?速度自然就上去了

View Code
using System.IO;
using System.Drawing;

/// <summary>
/// 图片上传
/// </summary>
/// <param name="uname">用户名,用于生成图片名称</param>
/// <param name="file">HttpPostedFile</param>
/// <returns>图片路径</returns>
public string[] ImgUpLoad(string uname, HttpPostedFile file)
{
string[] returnImg = new string[3];
int intThumbWidth = 160;
int intThumbHeight = 160;

string sFilename = "";
HttpPostedFile myFile = file;
int nFileLen = myFile.ContentLength;
if (nFileLen == 0)
return null;
//获取upImage选择文件的扩展名
string extendName = System.IO.Path.GetExtension(myFile.FileName).ToLower();
//判断是否为图片格式
if (extendName != ".jpg" && extendName != ".jpge" && extendName != ".gif" && extendName != ".bmp" && extendName != ".png")
return null;

byte[] myData = new Byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);
sFilename = System.IO.Path.GetFileName(myFile.FileName);

//生成目录名称
string dirName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();
if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath("~/PhotoUploadFile/ShowPhoto/" + dirName + "/")))
{
Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath("~/PhotoUploadFile/ShowPhoto/" + dirName + "/"));
}

//----生成未处理原图存放目录名称
string dirNameSrc = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();
if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath("~/PhotoUploadFile/Photo/" + dirNameSrc + "/")))
{
Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath("~/PhotoUploadFile/Photo/" + dirNameSrc + "/"));
}

DateTime dtime = DateTime.Now;
//生成图片文件名称
string fileName = dtime.Year.ToString()
+ dtime.Month.ToString()
+ dtime.Day.ToString()
+ dtime.Hour.ToString()
+ dtime.Minute.ToString()
+ dtime.Second.ToString()
+ dtime.Millisecond.ToString() + extendName;
fileName = uname + fileName;

//处理完显示图片
string savePath = "~/PhotoUploadFile/ShowPhoto/" + dirName + "/";
string dirPath = dirName + "/";

//----未处理原图路径与目录
string savePathSrc = "~/PhotoUploadFile/Photo/" + dirNameSrc + "/";
string dirPathSrc = dirNameSrc + "/";

//----保存未处理原图
System.IO.FileStream newFileSrc
= new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath(savePathSrc) + fileName,
System.IO.FileMode.Create, System.IO.FileAccess.Write);
newFileSrc.Write(myData, 0, myData.Length);
newFileSrc.Close();
returnImg[0] = savePathSrc + fileName;
//以上为上传原图

try
{
//原图加载
using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(savePathSrc + fileName)))
{
//原图宽度和高度
int width = sourceImage.Width;
int height = sourceImage.Height;
int smallWidth;
int smallHeight;

//获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽 和 原图的高/缩略图的高)
if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)
{
//smallWidth = width/(height/intThumbHeight);
smallWidth = width * intThumbHeight / height;
smallHeight = intThumbHeight;
}
else
{
smallWidth = intThumbWidth;
//smallHeight = height/(width/intThumbWidth);
smallHeight = height * intThumbWidth / width;
}

//生成目录名称
dirName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();
if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath("~/PhotoUploadFile/Thumb/" + dirName + "/")))
{
Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath("~/PhotoUploadFile/Thumb/" + dirName + "/"));
}

dtime = DateTime.Now;

savePath = "~/PhotoUploadFile/Thumb/" + dirName + "/";
string dirPath2 = dirName + "/";


//缩略图保存的绝对路径
string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(savePath) + fileName;

//新建一个图板,以最小等比例压缩大小绘制原图
using (System.Drawing.Image bitmap3 = new System.Drawing.Bitmap(smallWidth, smallHeight))
{
//绘制中间图
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap3))
{

//高清,平滑
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))
{
//绘制缩略图
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(bitmap3, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);

g.Dispose();
bitmap1.Save(smallImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
returnImg[1] = savePath + fileName;
}
}
}

//生成显示图片 ShowPhoto
intThumbWidth = 800;
intThumbHeight = 600;
string equalSavePath = "~/PhotoUploadFile/ShowPhoto/" + dirName + "/";
string showSrcPath = equalSavePath;
equalSavePath = System.Web.HttpContext.Current.Server.MapPath(equalSavePath) + fileName;
if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)
{
//smallWidth = width/(height/intThumbHeight);
smallWidth = width * intThumbHeight / height;
smallHeight = intThumbHeight;
}
else
{
smallWidth = intThumbWidth;
//smallHeight = height/(width/intThumbWidth);
smallHeight = height * intThumbWidth / width;
}
if (width <= 800 && height <= 600)
{
smallWidth = width;
smallHeight = height;
}
//if (height < 800 && width < 600)
//{
// smallWidth = height;
// smallHeight = width;
//}
//if (width <= 800) {
// smallWidth = width;
//}
//if (height <= 600) {
// smallHeight = height;
//}

//新建一个图板,以最小等比例压缩大小绘制原图
using (System.Drawing.Image bitmap3 = new System.Drawing.Bitmap(smallWidth, smallHeight))
{
//绘制中间图
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap3))
{

//高清,平滑
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))
{
//绘制缩略图
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(bitmap3, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);

g.Dispose();
bitmap1.Save(equalSavePath, System.Drawing.Imaging.ImageFormat.Jpeg);
returnImg[2] = showSrcPath + fileName;
}
}
}
}
}
catch (Exception ex)
{
//出错则删除
//System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(savePath));
throw ex;
}

return returnImg;
}

 

posted @ 2012-01-12 11:00  随风ˇ止步  阅读(549)  评论(0编辑  收藏  举报