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编解码参数保存到输出流。