如何在Asp.net的图像控件中显示数据库中的图像?

Hot*_*ter 0 .net c# asp.net

如何在Asp.net的图像控件中显示数据库中的图像?我们必须在asp.net页面中显示员工的图像及其详细信息,但问题是如何在asp.net图像控件上显示图像,因为图像控件通过属性ImageUrl来拍照。

请指导....

Ash*_*pta 5

您可以创建一个 HttpHandler(ashx) 页面,该页面将采用查询字符串并将其设置为图像控件的 imageUrl 属性

<asp:image id="imgEmployee" imageUrl="DisplayImage.ashx?employeeId=<someId>"/>
Run Code Online (Sandbox Code Playgroud)

现在在 DisplayImage.ashx 中,您可以重写 Processrequest,如下所示:-

    public void ProcessRequest (HttpContext context) 
    { 
          int employeeId;
          if (context.Request.QueryString["employeeId"] != null)
   employeeId = Convert.ToInt32(context.Request.QueryString["employeeId"]);
          else
            throw new ArgumentException("No parameter specified");

        byte[] imageData= ;// get the image data from the database using the employeeId Querystring
        Response.ContentType = "image/jpeg"; // You can retrieve this also from the database
        Response.BinaryWrite(imageData);

    } 
Run Code Online (Sandbox Code Playgroud)

Web.config 更改:-

<httpHandlers>
  <add verb="*" path="img/*" type="DisplayImage"/>
</httpHandlers>
Run Code Online (Sandbox Code Playgroud)

详细信息请参见此处此处

希望这可以帮助..