Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 处理程序图像显示空问题_C#_Asp.net_Sql_Httphandler - Fatal编程技术网

C# 处理程序图像显示空问题

C# 处理程序图像显示空问题,c#,asp.net,sql,httphandler,C#,Asp.net,Sql,Httphandler,我正在尝试创建处理程序,并试图让它在返回空值时返回默认图片。但是,当我使用下面的第二个代码位通过页面访问它时,我会得到一个错误图像。但是如果我直接转到页面Image.ashx?id=28,并且没有图像存在,我会得到默认图像,为什么它不显示在我的页面上?如果图像确实存在,它会在页面上显示该图像,如果直接调用 谢谢 My Image.ashx文件: public void ProcessRequest(HttpContext ctx) { string id = ct

我正在尝试创建处理程序,并试图让它在返回空值时返回默认图片。但是,当我使用下面的第二个代码位通过页面访问它时,我会得到一个错误图像。但是如果我直接转到页面Image.ashx?id=28,并且没有图像存在,我会得到默认图像,为什么它不显示在我的页面上?如果图像确实存在,它会在页面上显示该图像,如果直接调用

谢谢

My Image.ashx文件:

    public void ProcessRequest(HttpContext ctx)
    {
        string id = ctx.Request.QueryString["id"];

        SqlConnection con = new SqlConnection(CONN INFO);
        SqlCommand cmd = new SqlCommand("SELECT EventInviteImage FROM Events WHERE EventID = @EventID", con);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@EventID", id);

        byte[] pict = null; 

        try
        {
            con.Open();
            pict = (byte[])cmd.ExecuteScalar();            

            ctx.Response.ContentType = "image/pjpeg";
            ctx.Response.BinaryWrite(pict);
        }
        catch
        {
            ctx.Response.Write("<img src='/images/defaultevent.jpg'>");
        }
        finally
        {
            con.Close();
        }



    }

    public bool IsReusable
    {
        get
        {
            return true;
        }
    }
publicvoidprocessrequest(HttpContext-ctx)
{
string id=ctx.Request.QueryString[“id”];
SqlConnection con=新的SqlConnection(连接信息);
SqlCommand cmd=newsqlcommand(“从事件中选择EventInviteImage,其中EventID=@EventID”,con);
cmd.CommandType=CommandType.Text;
cmd.Parameters.AddWithValue(“@EventID”,id);
字节[]pict=null;
尝试
{
con.Open();
pict=(字节[])cmd.ExecuteScalar();
ctx.Response.ContentType=“image/pjpeg”;
ctx.Response.BinaryWrite(pict);
}
抓住
{
ctx.Response.Write(“”);
}
最后
{
con.Close();
}
}
公共布尔可重用
{
得到
{
返回true;
}
}
呼叫图像:

<img src="Image.ashx?id=28" />


处理程序需要将默认图像本身的二进制数据流化。不是HTML图像标记。@Martin当您引用二进制数据时,您并不是指我所采用的路径。你指的是什么,你能给我举个例子吗?@Martin如果我使用
ctx.Response.BinaryWrite(“path to defaultevent.jpg”)
ctx.Response.BinaryWrite(Server.MapPath(“~/images/defaultevent.jpg”)它说它有无效的参数。是。我想你可能需要
响应。WriteFile
代替。@Martin@Oded得到了它,它成功了,谢谢你,你需要
WriteFile
而不是
BinaryWrite
try
{
   ...
   ctx.Response.ContentType = "image/pjpeg";
   ctx.Response.BinaryWrite(pict);
}
catch
{
   ctx.Response.ContentType = "image/pjpeg";
   ctx.Response.Write("<img src='/images/defaultevent.jpg'>");
}
catch
{
  ctx.Response.WriteFile("path to defaultevent.jpg");
}