国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET中圖片顯示方法實例

2024-07-10 13:29:15
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了ASP.NET中圖片顯示方法,實例分析了ASP.NET圖片顯示所涉及的圖片路徑、縮略圖及更新數據庫圖片瀏覽次數等相關技巧,需要的朋友可以參考下
 

本文實例講述了ASP.NET中圖片的顯示方法。分享給大家供大家參考。具體如下:

genimage.ashx:

復制代碼代碼如下:
<%@ WebHandler Language="C#" Class="netpix.ImageGenerator" %>

 

genimage.ashx.cs:
 

  1. // Copyright (C) 2003 by Greg Ennis 
  2. // (mailto:greg@ennis.net) 
  3. // 
  4. // The contents of this file are subject to the Artistic License (the "License"). 
  5. // You may not use this file except in compliance with the License.  
  6. // You may obtain a copy of the License at: 
  7. // http://www.opensource.org/licenses/artistic-license.html 
  8. using System; 
  9. using System.Collections; 
  10. using System.ComponentModel; 
  11. using System.Data; 
  12. using System.Data.SqlClient; 
  13. using System.Drawing; 
  14. using System.Web; 
  15. using System.IO; 
  16. using System.Configuration; 
  17. using System.Web.SessionState; 
  18. using System.Web.UI; 
  19. using System.Web.UI.WebControls; 
  20. using System.Web.UI.HtmlControls; 
  21. namespace netpix 
  22.   public class ImageGenerator : IHttpHandler  
  23.   {  
  24.     public bool IsReusable  
  25.     { get { return true; } }  
  26.     public void ProcessRequest(HttpContext Context)  
  27.     {  
  28.       // Get the image filename and album root path from the database 
  29.       //圖片瀏覽次數 
  30.       int numviews; 
  31.       //圖片數據庫中的ID 
  32.       int picid = Convert.ToInt32(Context.Request["id"]); 
  33.       //圖片路徑  
  34.       string imgpath = npdata.GetPathToPicture(picid, out numviews); 
  35.       // Writing an image to output stream 
  36.       Context.Response.ContentType = "image/jpg"
  37.       // 'thumbnail' means we are requesting a thumbnail 
  38.       //顯示縮略圖 
  39.       if (Context.Request["thumbnail"] != null
  40.       { 
  41.         // Need to load the image, resize it, and stream to the client. 
  42.         // Calculate the scale so as not to stretch or distort the image. 
  43.         Bitmap bmp = new Bitmap(imgpath); 
  44.         float scale = 150.0f / System.Math.Max(bmp.Height, bmp.Width); 
  45.         System.Drawing.Image thumb = bmp.GetThumbnailImage((int)(bmp.Width * scale), (int)(bmp.Height * scale), null, System.IntPtr.Zero); 
  46.         thumb.Save(Context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); 
  47.         bmp.Dispose(); 
  48.         thumb.Dispose(); 
  49.       } 
  50.       else 
  51.       { 
  52.         // Stream directly from the file 
  53.         // Get the stream and send it out the response 
  54.         System.IO.FileStream fs = File.Open(imgpath, FileMode.Open, FileAccess.Read, FileShare.Read); 
  55.         const int byteLength = 8192; 
  56.         byte[] bytes = new byte[byteLength]; 
  57.         while( fs.Read(bytes, 0, byteLength ) != 0 ) 
  58.         { 
  59.           Context.Response.BinaryWrite(bytes);  
  60.         } 
  61.         fs.Close(); 
  62.         //更新數據庫瀏覽次數 
  63.         npdata.SetNumViews(picid, numviews+1); 
  64.       } 
  65.     } 
  66.   } 
?
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 聂拉木县| 乳源| 车致| 隆德县| 岳普湖县| 德江县| 嘉祥县| 岫岩| 怀仁县| 万安县| 商城县| 信宜市| 西畴县| 沐川县| 襄城县| 沽源县| 兰考县| 枝江市| 祁连县| 周宁县| 宜城市| 靖西县| 墨玉县| 杭锦后旗| 大理市| 府谷县| 阿拉善左旗| 文水县| 浙江省| 宁海县| 皋兰县| 上饶市| 神木县| 原阳县| 黄平县| 澄城县| 涞源县| 临城县| 陇南市| 台山市| 大丰市|