.net如何上传图片,并生成缩略图效果
2011年04月18日
很晚了。就不和大家废话了。搞了一晚才搞定。和大家分享一下啊:
请看代码:
protected void btnUpLoad_Click(object sender, EventArgs e)
{
//判断是否有文件提交
if (HttpContext.Current.Request.Files.Count != 0)
{
string path = HttpContext.Current.Request.ApplicationPath;
HttpPostedFile postFile = HttpContext.Current.Request.Files[0];
string filename = DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
SavePostedImage(postFile, path + "/UploadImg/", filename, 100, 100, System.Drawing.Imaging.ImageFormat.Jpeg, 0.8);
filename += "-isBig";
SavePostedImage(postFile, path + "/UploadImg/", filename, 502, 670, System.Drawing.Imaging.ImageFormat.Jpeg, 0.8);
postFile.InputStream.Close();
}
}
///
/// 上传图片 集成cmyk→rgb
///
/// 上载文件的访问
/// 保存目录
/// 新名字
/// 限高
/// 限宽
/// 图片格式要求
/// 压缩比(double:0--1)
/// 上传后的文件名
public string SavePostedImage(HttpPostedFile postedFile, string filePath, string fileName, int maxHeight, int maxWidth, System.Drawing.Imaging.ImageFormat imgFormat, double rate)
{
try
{
//判断压缩格式
if (imgFormat == null)
{
if (postedFile.FileName.ToLower().EndsWith("jpg"))
{
imgFormat = ImageFormat.Jpeg;
}
else if (postedFile.FileName.ToLower().EndsWith("gif"))
{
imgFormat = ImageFormat.Gif;
}
else if (postedFile.FileName.ToLower().EndsWith("bmp"))
{
imgFormat = ImageFormat.Bmp;
}
else if (postedFile.FileName.ToLower().EndsWith("png"))
{
imgFormat = ImageFormat.Png;
}
else if (postedFile.FileName.ToLower().EndsWith("tiff"))
{
imgFormat = ImageFormat.Tiff;
}
else if (postedFile.FileName.ToLower().EndsWith("icon"))
{
imgFormat = ImageFormat.Icon;
}
else
{
return "";
}
}
Bitmap bmp = new Bitmap(postedFile.InputStream);
bmp = IsCMYK(bmp) ? ConvertCMYK(bmp, rate) : bmp;
if ((bmp.HorizontalResolution > 72) || (bmp.VerticalResolution > 72))
{
bmp.SetResolution(72, 72);
}
Bitmap saveBmp;
//判断大小,进行压缩
if (bmp.Height > maxHeight)
{
if ((maxHeight == 0) || (maxWidth == 0))
{
saveBmp = new Bitmap(bmp);
}
else
{
Double heightRatio = Convert.ToDouble(maxHeight) / Convert.ToDouble(bmp.Height);
Double widthRatio = Convert.ToDouble(maxWidth) / Convert.ToDouble(bmp.Width);
Double scaleRatio;
scaleRatio = heightRatio > widthRatio ? widthRatio : heightRatio;
saveBmp = new Bitmap(bmp, Convert.ToInt32(bmp.Width * scaleRatio), Convert.ToInt32(bmp.Height * scaleRatio));
while (Convert.ToInt32(saveBmp.Height * scaleRatio) > maxHeight)
{
heightRatio = Convert.ToDouble(maxHeight) / Convert.ToDouble(bmp.Height);
widthRatio = Convert.ToDouble(maxWidth) / Convert.ToDouble(bmp.Width);
scaleRatio = heightRatio > widthRatio ? widthRatio : heightRatio;
saveBmp = new Bitmap(bmp, Convert.ToInt32(bmp.Width * scaleRatio), Convert.ToInt32(bmp.Height * scaleRatio));
}
}
}
else
{
saveBmp = new Bitmap(bmp);
}
DateTime DateFile = DateTime.Now;
string NewFileName = DateFile.Year.ToString() + DateFile.Month.ToString()+DateFile.Day.ToString() + DateFile.Hour.ToString() + DateFile.Minute.ToString();
fileName = fileName.LastIndexOf('.') > 0 ? fileName.Substring(0, fileName.IndexOf('.')) : fileName.Replace(".", "");
fileName = fileName != "" ? NewFileName + fileName : NewFileName;
string path = HttpContext.Current.Server.MapPath(filePath);
//如果不存在该文件夹
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
fileName += "." + imgFormat.ToString();
saveBmp.Save(path + "\\" + fileName, imgFormat);
bmp.Dispose();
saveBmp.Dispose();
}
catch (System.Exception)
{
fileName = "";
}
return fileName;
}
private bool IsCMYK(System.Drawing.Image img)
{
string FlagVals = GetImageFlags(img);
if ((FlagVals.IndexOf("Ycck") > -1) || (FlagVals.IndexOf("Cmyk") > -1))
{ return true; }
else
{ return false; }
}
private string GetImageFlags(System.Drawing.Image img)
{
ImageFlags FlagVals = (ImageFlags)Enum.Parse(typeof(ImageFlags), img.Flags.ToString());
return FlagVals.ToString();
}
private Bitmap ConvertCMYK(Bitmap bmp, double rate)
{
rate = rate > 1.0 ? 1.0 : rate;
rate = rate RGB了
g.DrawImage(bmp, rect);
Bitmap returnBmp = new Bitmap(tmpBmp);
g.Dispose();
tmpBmp.Dispose();
bmp.Dispose();
return returnBmp;
}
搞定收工。洗澡睡觉!
发表评论
-
CQ与其他聊天工具继承
2012-01-20 01:40 554CQ与其他聊天工具继承 2010年06月13日 VBSc ... -
隐藏命令行CMD框
2012-01-20 01:39 822隐藏命令行CMD框 2010年07月18日 vbs脚本: ... -
insert->Step Generator
2012-01-20 01:39 490insert->Step Generator 2010 ... -
[转]使用C#开发ActiveX控件全攻略
2012-01-20 01:39 762[转]使用C#开发ActiveX控 ... -
程式交易(一)大智慧函数(下)
2012-01-19 09:29 849程式交易(一)大智慧函数(下) 2011年07月24日 ... -
关于图像格式
2012-01-19 09:29 797关于图像格式 2010年06 ... -
学Photoshop前的19句经典忠告
2012-01-19 09:29 857学Photoshop前的19句经典 ... -
安全bios手册(5)
2012-01-19 09:29 555安全bios手册(5) 2010年06月20日 ... -
指针与引用的一角
2012-01-17 02:04 723指针与引用的一角 2011 ... -
【转】Android 的cpu 硬盘 内存 网络设置 系统信息 硬件信息
2012-01-17 02:04 603【转】Android 的cpu 硬盘 ... -
MD5算法实现注意点
2012-01-17 02:04 660MD5算法实现注意点 2011年03月02日 最近很有危 ... -
C++Union结构体转换成C#代码
2012-01-17 02:04 1712C++Union结构体转换成C#代码 2010年10月27日 ... -
日语词汇
2012-01-15 21:25 395日语词汇 2010年11月24日 时间变化:推移:すいい ... -
1级能力考试知识汇总
2012-01-15 21:25 6091级能力考试知识汇总 2009年06月04日 人物题包括 ... -
小儿时,我这样被剃头
2012-01-15 21:25 332小儿时,我这样被剃头 2 ... -
化村姑为女郎
2012-01-15 21:25 528化村姑为女郎 2009年07 ...
相关推荐
asp.net上传图片 并生成缩略图
Asp.net上传图片同时生成缩略图和水印图
本文详细介绍在ASP.NET中上传图片并生成缩略图 在ASP.NET中上传图片并生成缩略图!
原类生成的缩略图的问题在于:高或者宽可能会大于需求值。比如,我需要的图片宽为150,高为120,当原图 * 高为150,宽为150或差距不大时,生成的图片不会有变化,即也是高150,宽150,这样,当把这些图片显示到网页...
ASP.NET图片上传,生成缩略图,加文字、图片水印
上传图片 批量上传图片 生成缩略图 asp.net 代码
在asp.net中,上传图片功能或者是常用的,生成缩略图也是常用的。baidu或者google,c#的方法也是很多的,但是一用却发现缩略图不清晰啊,缩略图片太大之类的事情,下面是我在处理图片上的代码,效果不错,所以拿出来...
asp.net上传图片 生成缩略图 并保存到数据库 从数据库读取图片
【ASP.NET编程知识】ASP.NET实现上传图片并生成缩略图的方法.docx
asp.net上传图片+自动生成缩略图+图片水印功能
动态添加控件实现多文件上传,并对图片大文件进行压缩生成缩略图来保存,以节省空间和加快图片加载速度。
NULL 博文链接:https://weizhigan.iteye.com/blog/837975
asp.net图片上传生成缩略图和水印 asp.net教材,新手上路的武器
ASP.NET源码——上传图片生成缩略图、图片水印、文字水印.zip
ASP.Net 上传图片并生成高清晰缩略图的代码
可以根据不同参数生成缩略图片,而且不变形
Asp.net图片生成缩略图 Asp.net图片生成缩略图