- 浏览: 19927 次
- 性别:
- 来自: 成都
最新评论
.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;
}
搞定收工。洗澡睡觉!
发表评论
-
多线程加速图像模板匹配
2012-01-20 00:15 658多线程加速图像模板匹 ... -
硬件和体系结构
2012-01-20 00:15 451硬件和体系结构 2010年0 ... -
(翻译)Linux常用命令列表
2012-01-20 00:15 591(翻译)Linux常用命令列表 ... -
086_《版主答疑-Delphi高级编程技巧》
2012-01-20 00:15 568086_《版主答疑-Delphi高级编程技巧》 2010年1 ... -
ubuntu问题总结
2012-01-20 00:15 965ubuntu问题总结 2010年07 ... -
国内美女产地解说!
2012-01-19 01:06 424国内美女产地解说! 2011年05月07日 ... -
中国各省女孩美丽程度的比较分析
2012-01-19 01:06 554中国各省女孩美丽程度的比较分析 2011年06月22日 ... -
刘翔(上)你猜透了其中技术和要点,就可以玩得更好
2012-01-19 01:06 399刘翔(上)你猜透了其中 ... -
三大专业表演院校开考 艺考报名人数有增无减
2012-01-19 01:06 503三大专业表演院校开考 艺考报名人数有增无减 2011年02月 ... -
网络语言进课堂:上海禁止北京面对
2012-01-19 01:06 584网络语言进课堂:上海禁 ... -
Android Bitmap和Canvas学习笔记
2012-01-17 00:30 546Android Bitmap和Canvas学习笔记 2011 ... -
程式交易(一)大智慧函数(下)
2012-01-17 00:29 594程式交易(一)大智慧函数(下) 2011年07月24日 ... -
关于图像格式
2012-01-17 00:29 538关于图像格式 2010年06 ... -
学Photoshop前的19句经典忠告
2012-01-17 00:29 729学Photoshop前的19句经典 ... -
成人笑话大全第019篇
2012-01-15 14:49 446成人笑话大全第019篇 20 ... -
怎么样写议论文
2012-01-15 14:48 429怎么样写议论文 2009年09月25日 首 页 ┆ 教 ... -
学生操行评语
2012-01-15 14:48 402学生操行评语 2011年01月07日 优等生: 1 ... -
高中生自我评语 高中毕业生自我鉴定
2012-01-15 14:48 712高中生自我评语 高 ... -
笑话大全
2012-01-15 14:48 438笑话大全 2009年10月14日 不能随便起外号有个顽皮 ...
相关推荐
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图片生成缩略图