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

ASP.NET
asp.net(C#) 生成随机验证码的代码
C# 生成高质量缩略图程序—终极算法
iis的http 500内部服务器错误的解决
效控制C#中label输出文字的长度,自动换行
asp.net下生成99个不同的随机数
Asp.Net文本换行
Asp.Net中文本换行
asp.net 上传大文件解决方案
asp.net程序编译调试时偶尔出现访问被拒绝的错误的解决方法
控件开发时两种JS嵌入资源方式的使用方法
AspNetPager分页控件源代码(Version 4.2)
asp. net下使用foreach简化文本文件的访问。
asp.net下经典数据库记录分页代码
微软发布的Data Access Application Block的使用代码
如何在网站级别动态更改主题
asp.net下模态对话框关闭之后继续执行服务器端代码的问题
asp.net下利用JS实现对后台CS代码的调用方法
批量删除记录时如何实现全选方法总结
asp.net下OnClientClick的妙用!
[c#]asp.ent下开发中Tag的开发技巧

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 69 ::
收藏到网摘: 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();
}