当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET中高质量缩略图的生成代码

ASP.NET
Asp.Net 通用数据操作类 (附通用数据基类)
asp.net汉字转拼音和获取汉字首字母的代码
asp.net 多字段模糊查询代码
OpenCms 带分页的新闻列表
URLRewriter最简单入门介绍 URLRewriter相关资源
asp.net Repeater取得CheckBox选中的某行某个值
asp.net清空Cookie的两种方法
asp.net一些很酷很实用的.Net技巧
asp.net生成高质量缩略图通用函数(c#代码),支持多种生成方式
asp.net TripleDES加密、解密算法
Asp.net中防止用户多次登录的方法
asp.net Repeater取得CheckBox选中的某行某个值的c#写法
asp.net DataGridView导出到Excel的三个方法[亲测]
C#,winform,ShowDialog,子窗体向父窗体传值
asp.net学习中发现的比较完整的流程
ASP.net 页面被关闭后,服务器端是否仍然执行中?
asp.net Context.Handler 页面间传值方法
asp.net xml序列化与反序列化
asp.net实例代码protected override void Render(HtmlTextWriter writer)
asp.net(c#)捕捉搜索引擎蜘蛛和机器人

ASP.NET中高质量缩略图的生成代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 78 ::
收藏到网摘: n/a

private Size NewSize(int maxWidth, int maxHeight, int width, int height)
{
double w = 0.0;
double h = 0.0;
double sw = Convert.ToDouble(width);
double sh = Convert.ToDouble(height);
double mw = Convert.ToDouble(maxWidth);
double mh = Convert.ToDouble(maxHeight);
if ( sw < mw && sh < mh )
{
w = sw;
h = sh;
}
else if ( (sw/sh) > (mw/mh) )
{
w = maxWidth;
h = (w * sh)/sw;
}
else
{
h = maxHeight;
w = (h * sw)/sh;
}
return new Size(Convert.ToInt32(w), Convert.ToInt32(h));
}
private void SendSmallImage(string fileName, int maxWidth, int maxHeight)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(fileName));
System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;
Size newSize = NewSize(maxWidth, maxHeight, img.Width, img.Height);
Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);
Graphics g = Graphics.FromImage(outBmp);
// 设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height),
0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
g.Dispose();
if (thisFormat.Equals(ImageFormat.Gif))
{
Response.ContentType = "image/gif";
}
else
{
Response.ContentType = "image/jpeg";
}
// 以下代码为保存图片时,设置压缩质量
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x];//设置JPEG编码
break;
}
}
if (jpegICI != null)
{
outBmp.Save(Response.OutputStream, jpegICI, encoderParams);
}
else
{
outBmp.Save(Response.OutputStream, thisFormat);
}
img.Dispose();
outBmp.Dispose();
}