当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 一个.net 压缩位图至JPEG的代码

ASP.NET
用 Asp.Net 建立一个在线 RSS 新闻聚合器的方法
关于前台调用后台事件__doPostBack函数
Bin 和 App_Code 文件夹介绍
.NET 2.0 的压缩功能代码
解决Visual Studio 2005 无法显示设计视图的方法
asp.net(c#)两种随机数的算法,可用抽考题
asp.net下url传递中文的解决方案
XmlReader 读取器读取内存流 MemoryStream 的注意事项
asp.net下创建、查询、修改带名称空间的 XML 文件的例子
使用.NET存储XML数据的方法
XslTransform.Transform将结果输出到字符串里的方法
安装 VS2005 SP1 有关问题的解决办法
asp.net下中文验证码,免费开源代码
自定义应用程序配置文件(app.config)
asp.net下使用DIME协议上传文件
动态改变ASP.net页面标题和动态指定页面样式表的方法
WEB上调用HttpWebRequest奇怪问题的解决方法
HTTP协议下用Web Service上传大文件的解决方案
asp.net下Response.ContentType类型汇总
ASP.NET User Control使用技巧一则

ASP.NET 中的 一个.net 压缩位图至JPEG的代码


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

bmp.aspx

首先准备一张位图图像source.bmp,将它保存在bmp.aspx同一目录中


<%@Pagelanguage="c#"%>
<%@ImportNamespace="System.Drawing"%>
<%@ImportNamespace="System.Drawing.Imaging"%>

<scriptlanguage="c#"runat="server">

privatevoidPage_Load(objectsender,System.EventArgse)
{

//设置mime类型为image/jpeg,即将向浏览器输出JPGE格式的图像
Response.Clear();
Response.ContentType="image/jpeg";


BitmapOutputBitmap=newBitmap(Server.MapPath("source.bmp"));//新建BitMap对象
System.Drawing.Imaging.EncoderParametersencoderParams=newSystem.Drawing.Imaging.EncoderParameters();
long[]quality=newlong[1];

intcomp=0;
if(Request.QueryString["comp"]!=""){comp=Convert.ToInt16(Request.QueryString["comp"]);}
quality[0]=comp;//0to100最高质量为100
System.Drawing.Imaging.EncoderParameterencoderParam=newSystem.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality,quality);
encoderParams.Param[0]=encoderParam;

ImageCodecInfo[]arrayICI=ImageCodecInfo.GetImageEncoders();//获得包含有关内置图像编码解码器的信息的ImageCodecInfo对象。
ImageCodecInfojpegICI=null;
for(intx=0;x<arrayICI.Length;x++)
{
if(arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI=arrayICI[x];//设置JPEG编码
break;
}
}

if(jpegICI!=null)
{
OutputBitmap.Save(Response.OutputStream,jpegICI,encoderParams);//将位图对象以流格式并用JPEG编解码参数保存到输出流。

}

//cleanup
OutputBitmap.Dispose();

}
</script>


在浏览器地址输入:http://localhost/bmp.aspx?comp=0
将会看到图像,调整comp的值,将会看到不同的效果.