C# 如何动态设置asp控件的imageurl。我的图像以二进制形式存在于数据库中

C# 如何动态设置asp控件的imageurl。我的图像以二进制形式存在于数据库中,c#,asp.net,C#,Asp.net,我有一个ashx文件,我想通过它获取asp图像控件的imageurl。下面的代码是ashx文件 SqlConnection con = null; SqlDataReader dr = null; SqlCommand cmd = null; try { con = new SqlConnection(ConfigurationManager.ConnectionStrings["GalleriaDatabaseConn

我有一个ashx文件,我想通过它获取asp图像控件的imageurl。下面的代码是ashx文件

  SqlConnection con = null;
        SqlDataReader dr = null; SqlCommand cmd = null;
        try
        {
            con = new SqlConnection(ConfigurationManager.ConnectionStrings["GalleriaDatabaseConnectionString"].ConnectionString);
            cmd = new SqlCommand("Select Image from tblImage where UserName='" + context.Session["uname"] + "'");
            con.Open();
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                context.Response.ContentType = "image/jpg";
                context.Response.BinaryWrite((byte[])dr["Image"]);
                dr.Close();
            }
        }**

在ImageUr属性中提及ashx文件的路径。如下

<asp:Image runat='server' Id='image' ImageUrl='imageprocessor.ashx'></asp:Image>


Ashx是一个web处理程序,它提供一些服务,我假设在您的例子中,它提供一个图像。如果您将图像Id放入url并更改代码,则它可以使用url为图像提供服务。我不知道你想在这里做什么。ImageId??我不明白。数据库中只有图像和名称属性。我尝试将名称值作为图像url命名为“”bt。我没有从数据库中获取任何图像。by
ImageId
我指的是任何可用于标识要检索的图像的唯一标识符。当然,您需要更改查询,以便在从数据库获取图像时使用该查询。