繁体   English   中英

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

[英]How to show a image in database in the image control of Asp.net?

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

请指导。

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

<asp:image id="imgEmployee" imageUrl="DisplayImage.ashx?employeeId=<someId>"/>

现在在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);

    } 

Web.config更改:-

<httpHandlers>
  <add verb="*" path="img/*" type="DisplayImage"/>
</httpHandlers>

这里这里的细节。

希望这可以帮助..

这也可以在不创建处理程序的情况下完成。

//get the image from the database as byte array
byte[] image = (byte[])dr["image"];

//set the ImageUrl of the Image Control as a Base64 string
Image1.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(image)

或者,如果还需要with和height,则使用MemoryStream创建Image并获取image属性。

using (MemoryStream ms = new MemoryStream(image))
{
    System.Drawing.Image imageFromDB = System.Drawing.Image.FromStream(ms);

    Image1.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(image);
    Image1.Width = imageFromDB.Width;
    Image1.Height = imageFromDB.Height;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM