zoukankan      html  css  js  c++  java
  • 用Generic Handler(ashx)去显示非二进制的图片

    一般情况,显示非二进制的图片(存放在磁盘上的图片文件),直接用图片控件轻易实现。

    <img alt="" src="xxx.jpg" />

    <asp:Image ID="Image1" runat="server" ImageUrl="xxx.jpg" />

    由于程序要求,需要把图片文件转为数据流(二进制),再进行显示。因此想起使用Generic Handler(ashx)来处理。

    你可以参考下面代码:

    View Code
    <%@ WebHandler Language="C#" Class="ViewImage" %>

    using System;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Web;
    using System.Web.UI.WebControls;

    public class ViewImage : IHttpHandler
    {
        
    public void ProcessRequest(HttpContext context)
        {
            
    //接收图片路径
            string parameter = context.Request.QueryString["file"];      
            
    //使用UrlDecode解编码  
            string path = context.Server.MapPath(HttpUtility.UrlDecode(parameter));
            
    //转为字节
            byte[] datas = System.IO.File.ReadAllBytes(path);
            
    //输出数据流
            context.Response.OutputStream.Write(datas, 0, datas.Length);
        }
     
        
    public bool IsReusable
        {
            
    get
            {
                
    return false;
            }
        }
    }

      

    xxx.NavigateUrl = "~/ViewImage.ashx?file=" + fileFullPath;
  • 相关阅读:
    弱网测试—Network-Emulator-Toolkit工具
    chmod修改权限
    mysql:on duplicate key update与replace into
    mysql:批量更新
    linux:磁碟与档案系统管理
    linux:指令与档案的搜索
    linux:问题
    linux:档案与目录管理
    linux:档案权限
    linux:习惯
  • 原文地址:https://www.cnblogs.com/insus/p/2003740.html
Copyright © 2011-2022 走看看