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

时间:2017-07-08 18:04:03

标签: c# asp.net

我有一个ashx文件,通过它我想获得一个asp图像控件的图像。下面的代码是一个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();
            }
        }**

1 个答案:

答案 0 :(得分:0)

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

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