檢視 ImageServices 的原始碼
←
ImageServices
跳至導覽
跳至搜尋
由於下列原因,您沒有權限進行編輯此頁面的動作:
您請求的操作只有這個群組的使用者能使用:
使用者
您可以檢視並複製此頁面的原始碼。
==用途== * Web-Server端,圖檔縮放。(透過'''GDI+'''做圖檔縮放的計算) ==程式碼== <source lang="csharp"> <%@ WebHandler Language="C#" Class="ImageServices" %> using System; using System.Web; using System.Drawing; using System.Drawing.Imaging; public class ImageServices : IHttpHandler { public void ProcessRequest (HttpContext context) { //TODO: void ProcessRequest (HttpContext context) //來源參數 string strSrcPath = context.Request["src"]; //寬度參數 string strWidth = context.Request["width"]; //高度參數 string strHeight = context.Request["height"]; //輸出品質 string strQuality = context.Request["quality"]; //是否使用比例刻度 string strScale = context.Request["scale"]; //載入來源 string strFile = context.Server.MapPath(strSrcPath); using (Bitmap bmpSrc = new Bitmap(strFile)) { int nWidthSrc = bmpSrc.Width; int nHeightSrc = bmpSrc.Height; //參數解析(是否使用比例刻度) bool bIsScale; try { bIsScale = bool.Parse(strScale); } catch { bIsScale = false; } //參數解析(寬度) int nWidth; try { if (bIsScale == true) { nWidth = int.Parse(strWidth) * nWidthSrc / 100; } else { nWidth = int.Parse(strWidth); } } catch { nWidth = nWidthSrc; } //參數解析(高度) int nHeight; try { if (bIsScale == true) { nHeight = int.Parse(strHeight) * nHeightSrc / 100; } else { nHeight = int.Parse(strHeight); } } catch { nHeight = nHeightSrc; } //參數解析(輸出品質) int nQuality; try { nQuality = int.Parse(strQuality); } catch { nQuality = 100; } using (Bitmap bmpDest = new Bitmap(nWidth, nHeight, PixelFormat.Format24bppRgb)) { using (Graphics grap = Graphics.FromImage(bmpDest)) { Rectangle rectSrc = new Rectangle(0, 0, nWidthSrc, nHeightSrc); Rectangle rectDest = new Rectangle(0, 0, nWidth, nHeight); grap.DrawImage(bmpSrc, rectDest, rectSrc, GraphicsUnit.Pixel); //設定輸出類型 context.Response.ContentType = "image/Jpeg"; //處理圖形品質 ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo ici = null; //找出Encoder int nLength = codecs.Length; for (int i = 0; i < nLength; i++) { ImageCodecInfo codec = codecs[i]; if (codec.MimeType == "image/jpeg") { ici = codec; } } //參數 - 高品質圖檔 EncoderParameters ep = new EncoderParameters(); ep.Param[0] = new EncoderParameter(Encoder.Quality, nQuality); if ((ici == null) || (ep == null)) { //儲存-低品質 bmpDest.Save(context.Response.OutputStream, ImageFormat.Jpeg); } else { //儲存-高品質 bmpDest.Save(context.Response.OutputStream, ici, ep); } //結束 context.Response.End(); } } } } public bool IsReusable { get { return false; } } } </source> ==使用範例== <pre> * 例如圖放在 http://127.0.0.1/images/test.jpg * 這隻 ImageServices.ashx 放在 http://127.0.0.1/ImageServices/ImageServices.ashx 比例模式 * 長寬各70% http://127.0.0.1/ImageServices/ImageServices.ashx?src=/images/test.jpg&width=70&height=70&scale=true * 長寬各60% http://127.0.0.1/ImageServices/ImageServices.ashx?src=/images/test.jpg&width=60&height=60&scale=true 像點模式 * 長寬各500pixel http://127.0.0.1/ImageServices/ImageServices.ashx?src=/images/test.jpg&width=500&height=500&scale=false * 長寬各1000pixel http://127.0.0.1/ImageServices/ImageServices.ashx?src=/images/test.jpg&width=1000&height=1000&scale=false </pre> [[Category:泛型處理常式(ASP.NET)]] [[Category:GDI+]]
返回「
ImageServices
」頁面
導覽選單
個人工具
登入
命名空間
頁面
討論
變體
視圖
閱讀
檢視原始碼
檢視歷史
更多
搜尋
導覽
首頁
社群入口
新聞動態
新聞直播
近期變更
Online IDE
分類
程式語言
資料結構
演算法
技術
軟體
SQL資料庫
網站套件
免費資源
副檔名
檔案格式
分類
其他
動畫
漫畫
小說
小說導讀
驅動程式
購物用
求職網站
推薦書單
線上學習
技術類News或部落格
Microsoft devblogs
網頁技術News
資料技術News
安全性相關News
MS-MVP
藍色小舖認證專家
經營行銷類部落格
工具
連結至此的頁面
相關變更
特殊頁面
頁面資訊