如題:
添加命名空間如下:
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
-----------------------------------------------------------------------------------------------
顯示原圖:
//顯示原圖<br /> protected void btnImage_Click(object sender, EventArgs e)<br /> {<br /> string filePath = Server.MapPath("~/Up/美女.jpg");<br /> //方式一:<br /> //Response.ContentType = "image/jpeg";<br /> //Response.WriteFile(filePath);</p><p> //方式二:<br /> FileStream imageStr = new FileStream(filePath, FileMode.Open);<br /> byte[] imageData=new byte[imageStr.Length];<br /> imageStr.Read(imageData,0,(int)imageStr.Length);<br /> Response.OutputStream.Write(imageData, 0, (int)imageStr.Length);</p><p> }
想要改變的尺寸:
private static Size NewSize(int maxWidth, int maxHeight, int Width, int Height)<br /> {<br /> double w = 0.0;<br /> double h = 0.0;<br /> double sw = Convert.ToDouble(Width);<br /> double sh = Convert.ToDouble(Height);<br /> double mw = Convert.ToDouble(maxWidth);<br /> double mh = Convert.ToDouble(maxHeight);</p><p> if (sw < mw && sh < mh)//如果maxWidth和maxHeight大於源映像,則縮圖的長和高不變<br /> {<br /> w = sw;<br /> h = sh;<br /> }<br /> else if ((sw / sh) > (mw / mh))<br /> {<br /> w = maxWidth;<br /> h = (w * sh) / sw;<br /> }<br /> else<br /> {<br /> h = maxHeight;<br /> w = (h * sw) / sh;<br /> }<br /> return new Size(Convert.ToInt32(w), Convert.ToInt32(h));<br /> }
轉化成縮圖:
//轉化成縮圖<br /> public void SendSmallImage(string filename, string newfile, int maxHeight, int maxWidth)<br /> {<br /> System.Drawing.Image img = System.Drawing.Image.FromFile(filename);//源映像的資訊<br /> System.Drawing.Imaging.ImageFormat thisformat = img.RawFormat; //源映像的格式</p><p> Size newSize = NewSize(maxWidth, maxHeight, img.Width, img.Height); //返回調整後的映像Width與Height<br /> Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);<br /> Graphics g = Graphics.FromImage(outBmp);<br /> //設定畫布的描繪品質(高)<br /> g.CompositingQuality = CompositingQuality.HighQuality;<br /> g.SmoothingMode = SmoothingMode.HighQuality;<br /> g.InterpolationMode = InterpolationMode.HighQualityBicubic;<br /> g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);<br /> g.Dispose();<br /> //以下代碼為儲存圖片時,設定壓縮品質<br /> EncoderParameters encoderParams = new EncoderParameters();<br /> long[] quality = new long[1];<br /> quality[0] = 100;<br /> EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);<br /> encoderParams.Param[0] = encoderParam;<br /> //擷取包含有關內建映像編碼解碼器的資訊的ImageCodecInfo對象。<br /> ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();<br /> ImageCodecInfo jpegICI = null;<br /> for (int x = 0; x < arrayICI.Length; x++)<br /> {<br /> if (arrayICI[x].FormatDescription.Equals("JPEG"))<br /> {<br /> jpegICI = arrayICI[x];//設定jpeg編碼<br /> break;<br /> }<br /> }<br /> if (jpegICI != null)<br /> {<br /> outBmp.Save(newfile , jpegICI, encoderParams);<br /> }<br /> else<br /> {<br /> outBmp.Save(newfile, thisformat);<br /> }<br /> img.Dispose();<br /> outBmp.Dispose();<br /> }
頁面上是一個FileUpload控制項,先上傳原圖,在顯示在頁面上,添加一個Image控制項,寬,高設為:100×100,用以對比縮圖。
將原圖輸出縮減為100×100;