asp.net顯示高清縮圖

來源:互聯網
上載者:User

如題:

 

添加命名空間如下:

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;

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.