C# 如何将.ashx处理程序与asp:Image对象一起使用?

C# 如何将.ashx处理程序与asp:Image对象一起使用?,c#,asp.net,image,ashx,C#,Asp.net,Image,Ashx,我有一个ashx处理程序: <%@ WebHandler Language="C#" Class="Thumbnail" %> using System; using System.Web; public class Thumbnail : IHttpHandler { public void ProcessRequest(HttpContext context) { string imagePath = context.Request.Quer

我有一个ashx处理程序:

<%@ WebHandler Language="C#" Class="Thumbnail" %>

using System;
using System.Web;

public class Thumbnail : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string imagePath = context.Request.QueryString["image"];

        // split the string on periods and read the last element, this is to ensure we have
        // the right ContentType if the file is named something like "image1.jpg.png"
        string[] imageArray = imagePath.Split('.');

        if (imageArray.Length <= 1)
        {
            throw new HttpException(404, "Invalid photo name.");
        }
        else
        {
            context.Response.ContentType = "image/" + imageArray[imageArray.Length - 1];
            context.Response.Write(imagePath);
        }
    }

    public bool IsReusable
    {
        get { return true; }
    }
}
当我查看网页时,图像不会显示,HTML会准确显示我键入的内容:

<img class="thumbnail" src="Thumbnail.ashx?image=../Files%5Crandom guid string%5Cimages%5Ctest.jpg" style="border-width:0px;" />


有人能告诉我为什么这不起作用吗?不幸的是,我昨天才开始使用ASP.NET,我不知道它是如何工作的,因此请尽可能简单地解释,谢谢。

您打印的是图像的路径,而不是实际的图像内容。使用

context.Response.WriteFile(context.Server.MapPath(imagePath));
方法


确保将路径限制在某个安全位置。否则,您可能会引入安全漏洞,因为用户将能够看到服务器上任何文件的内容。

我就是最初的海报,我最终解决了问题。我所要做的就是改变:

<asp:Image ID="Image1" runat="server" CssClass="thumbnail" />

致:


标点符号需要更改为它的HTML等价物,所以反斜杠变成%5C等等,否则它将无法工作


哦,我忘了提了,这在数据列表中。使用
Image1.ImageUrl=“Thumbnail.ashx?image=blahblah”工作得非常好。

此代码有助于按指定路径中的文件名查看图像。
例如

您可以在您的ASPX html正文中使用.ashx。

例如:




/// 
///由Rajib Chowdhury暴徒创建。01766-306306; 网状物:http://www.rajibs.tk
///电邮:mysuccessstairs@hotmail.com
///FB:https://www.facebook.com/fencefunny
/// 
///于2014年12月12日完成本页编码
///Visual Studio 2013为Web编程C#
///网络版4.5
///数据库病毒MSSQL服务器2005
/// 
使用制度;
使用System.IO;
使用System.Web;
公共类Fimageview:IHttpHandler{
公共void ProcessRequest(HttpContext上下文)
{
HttpResponse r=context.Response;
r、 ContentType=“image/png”;
字符串文件=context.Request.QueryString[“FImageName”];
if(string.IsNullOrEmpty(文件))
{
context.Response.Redirect(“~/default”);//如果RequestQueryString为Null或为空
}
尝试
{
如果(文件==“”)
{
Redirect(“~/Error/PageNotFound”);
}
其他的
{
r、 WriteFile(“/Catalog/Images/”+file);//找到文件名时的图像路径
}
}
捕获(例外)
{
context.Response.ContentType=“image/png”;
r、 WriteFile(“/yourImagePath/NoImage.png”);//如果找不到文件名,则为图像路径
}
}
公共布尔可重用
{
得到
{
返回false;
}
}
}

实际上,我想要的只是路径,因为我想设置
img src
。稍后,我计划让HttpHandler生成一个缩略图,然后返回指向它的路径。这不是HTTP处理程序所做的。浏览器只是将处理程序视为图像文件。它无法更改页面的原始HTML响应。您的示例非常好。这是我见过的最好的解决办法
context.Response.WriteFile(context.Server.MapPath(imagePath));
<asp:Image ID="Image1" runat="server" CssClass="thumbnail" />
<asp:Image ID="Image1" runat="server" CssClass="thumbnail" imageurl='<%# "~/Thumbnail.ashx?image=" + Utilities.ToURLEncoding("~\\Files" + DataBinder.Eval(Container.DataItem, "file_path")) %>' />
<image src="/Timageview.ashx?TImageName=FrozenToront.Jpg"
                                      width="100" height="75" border="1"/>
    <%@
        WebHandler Language="C#"
        Class="Fimageview"
    %>

    /// <summary>
    /// Created By Rajib Chowdhury Mob. 01766-306306; Web: http://www.rajibs.tk
    /// Email: mysuccessstairs@hotmail.com
    /// FB: https://www.facebook.com/fencefunny
    /// 
    /// Complete This Page Coding On Fabruary 12, 2014
    /// Programing C# By Visual Studio 2013 For Web
    /// Dot Net Version 4.5
    /// Database Virsion MSSQL Server 2005
    /// </summary>

    using System;
    using System.IO;
    using System.Web;

public class Fimageview : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {

        HttpResponse r = context.Response;
        r.ContentType = "image/png";
        string file = context.Request.QueryString["FImageName"];
        if (string.IsNullOrEmpty(file))
        {
            context.Response.Redirect("~/default");//If RequestQueryString Null Or Empty
        }
        try
        {
            if (file == "")
            {
                context.Response.Redirect("~/Error/PageNotFound");
            }
            else
            {
                r.WriteFile("/Catalog/Images/" + file); //Image Path If File Name Found
            }
        }
        catch (Exception)
        {
            context.Response.ContentType = "image/png";
            r.WriteFile("/yourImagePath/NoImage.png");//Image Path If File Name Not Found
        }
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}