一个.net 压缩位图至JPEG的代码
点击次数:28 次 发布日期:2008-11-26 12:50:23 作者:源代码网
|
源代码网推荐 源代码网推荐 作者:淘特网 源代码网推荐 出处:淘特网 源代码网推荐 源代码网推荐 注:转载请注明出处 源代码网推荐 源代码网推荐 首先准备一张位图图像source.bmp,将它保存在bmp.aspx同一目录中 源代码网推荐 源代码网推荐 <%@ Page language="c#" %> 源代码网推荐 <%@ Import Namespace="System.Drawing" %> 源代码网推荐 <%@ Import Namespace="System.Drawing.Imaging" %> 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 <script language="c#" runat="server"> 源代码网推荐 源代码网推荐 private void Page_Load(object sender, System.EventArgs e) 源代码网推荐 { 源代码网推荐 源代码网推荐 // 设置 mime 类型为image/jpeg,即将向浏览器输出JPGE格式的图像 源代码网推荐 Response.Clear(); 源代码网推荐 Response.ContentType="image/jpeg"; 源代码网推荐 源代码网推荐 源代码网推荐 Bitmap OutputBitmap = new Bitmap(Server.MapPath("source.bmp"));//新建BitMap对象 源代码网推荐 System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters(); 源代码网推荐 long[] quality = new long[1]; 源代码网推荐 源代码网推荐 int comp = 0; 源代码网推荐 if (Request.QueryString["comp"] != "") { comp = Convert.ToInt16(Request.QueryString["comp"]); } 源代码网推荐 quality[0] = comp; //0 to 100 最高质量为100 源代码网推荐 System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); 源代码网推荐 encoderParams.Param[0] = encoderParam; 源代码网推荐 源代码网推荐 ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。 源代码网推荐 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) 源代码网推荐 { 源代码网推荐 OutputBitmap.Save(Response.OutputStream, jpegICI, encoderParams);//将位图对象以流格式并用JPEG编解码参数保存到输出流。 源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 // clean up 源代码网推荐 OutputBitmap.Dispose(); 源代码网推荐 源代码网推荐 } 源代码网推荐 </script> 源代码网推荐 在浏览器地址输入:http://localhost/bmp.aspx?comp=0 源代码网推荐 将会看到图像,调整comp的值,将会看到不同的效果. 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
