这里是我的源码
- C# code
using System.Drawing.Imaging;using System.Drawing;public partial class A_T_userpicture : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string fileName = ""; string filePath = ""; //清除输出 Response.Clear(); System.Drawing.Image img = null; if (Request.QueryString["fsrc"] != null) { fileName = Request.QueryString["fsrc"].ToString(); if (fileName.StartsWith("http://")) { filePath =fileName; //获取URL流 System.Net.WebClient client = new System.Net.WebClient(); System.IO.Stream stream = new System.IO.MemoryStream(); stream = client.OpenRead(filePath); //从流中得到img对象 img = System.Drawing.Image.FromStream(stream, true); } else { filePath = Server.MapPath(fileName); img = System.Drawing.Image.FromFile(filePath); } } else { try { filePath = Server.MapPath("mumu.JPG"); img = System.Drawing.Image.FromFile(filePath); } catch(Exception se) { throw se; //return; } } //初始化图片的宽和高 int fw =120; int fh =120; if (Request.QueryString["fw"] != null && Request.QueryString["fh"]!=null) { fw = int.Parse(Request.QueryString["fw"].ToString()); fh = int.Parse(Request.QueryString["fh"].ToString()); } System.Drawing.Bitmap outBmp = new System.Drawing.Bitmap(fw,fh); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(outBmp); try { ImageFormat thisformat = img.RawFormat; System.Drawing.Size newSize = this.getNewSize(fw, fh, img.Width, img.Height); //设置画布质量 //线性质量设置 ( AssumeLinear ) 高速度呈现。 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed; //指定高速度呈现 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed; //指定高质量的双三次插值法。执行预筛选以确保高质量的收缩。此模式可产生质量最高的转换图像。 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //清除绘图并指定背景色 g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制指定的 Image 的指定部分。 g.DrawImage(img,new System.Drawing.Rectangle((fw-newSize.Width)/2,(fh-newSize.Height)/2,newSize.Width,newSize.Height),0,0,img.Width, img.Height, System.Drawing.GraphicsUnit.Pixel); System.IO.MemoryStream ms = new System.IO.MemoryStream(); //特定图像编码器,编码方式 EncoderParameters parameters = new EncoderParameters(2); parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, ((long)90)); parameters.Param[1] = new EncoderParameter(Encoder.Compression, ((long)1024)); System.Drawing.Imaging.ImageCodecInfo jpegICI = null; //检索有关已安装的图像编码解码器的所有相关信息 System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders(); for (int i = 0; i < arrayICI.Length - 1; i++) { if (arrayICI[i].FormatDescription.Equals("JPEG")) { jpegICI = arrayICI[i]; outBmp.Save(ms, jpegICI, parameters); } else { outBmp.Save(ms, thisformat); } } Response.ClearContent(); #region 响应类型设置 if (thisformat.Equals(System.Drawing.Imaging.ImageFormat.Gif)) { Response.ContentType = "image/Gif"; } else if (thisformat.Equals(System.Drawing.Imaging.ImageFormat.Wmf)) { Response.ContentType = "image/Wmf"; } else if (thisformat.Equals(System.Drawing.Imaging.ImageFormat.Bmp)) { Response.ContentType = "image/bmp"; } else if (thisformat.Equals(System.Drawing.Imaging.ImageFormat.Emf)) { Response.ContentType = "image/Emf"; } else if (thisformat.Equals(System.Drawing.Imaging.ImageFormat.Exif)) { Response.ContentType = "image/Exif"; } else if (thisformat.Equals(System.Drawing.Imaging.ImageFormat.Icon)) { Response.ContentType = "image/Icon"; } else if (thisformat.Equals(System.Drawing.Imaging.ImageFormat.Png)) { Response.ContentType = "image/Png"; } else if (thisformat.Equals(System.Drawing.Imaging.ImageFormat.Tiff)) { Response.ContentType = "image/Tiff"; } else { Response.ContentType = "image/jpeg"; } #endregion //写入流 Response.BinaryWrite(ms.ToArray()); parameters.Dispose(); outBmp.Dispose(); g.Dispose(); } catch(Exception se) { outBmp.Dispose(); g.Dispose(); throw se; } } } //自适应图片大小 private System.Drawing.Size getNewSize(int maxwidth,int maxheight,int width,int height) { try { 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 System.Drawing.Size(Convert.ToInt32(W),Convert.ToInt32(H)); } catch(Exception se) { throw se; } }}