⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mshandler.ashx.txt

📁 《圣殿祭司的ASP.NET 2.0开发详解——使用C#》光盘内容.包含了书籍所含的源代码.非常经典的一本asp.net2.0的书籍
💻 TXT
字号:
<%@ WebHandler Language="C#" Class="MsHandler" %>
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Web.Caching;

public class MsHandler : IHttpHandler 
{

    public void ProcessRequest (HttpContext context) 
    {
        // Get the image ID from querystring, and use it to generate a cache key.
        String imageID = context.Request.QueryString["PhotoID"];
        String cacheKey = context.Request.CurrentExecutionFilePath + ":" + imageID;
        Byte[] imageBytes;

        // Check if the cache contains the image.
        Object cachedImageBytes = context.Cache.Get(cacheKey);
        if (cachedImageBytes != null)
        {
            imageBytes = (Byte[])cachedImageBytes;
        }

        else

        {
            // Get image from business layer, and save it into a Byte array as JPEG.
            Image im = PhotoLibrary.GetImage("PhotoID");
            MemoryStream stream = new MemoryStream();
            im.Save(stream, ImageFormat.Jpeg);
            stream.Close();
            im.Dispose();
            imageBytes = stream.GetBuffer();
            
            // Store it in the cache (to be expired after 2 hours).
            context.Cache.Add(cacheKey, imageBytes, null,
            DateTime.MaxValue, new TimeSpan(2, 0, 0),
            CacheItemPriority.Normal, null);
        }

        // Send back image.
        context.Response.ContentType = "image/jpeg";
        context.Response.Cache.SetCacheability(HttpCacheability.Public);
        context.Response.BufferOutput = false;
        context.Response.OutputStream.Write(imageBytes, 0, imageBytes.Length);

    }
    
    public bool IsReusable 
    {

        get 
        {
            return false;
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -