简体   繁体   English

从数据库在网络表单上显示图像

[英]Display Image on webform from Database

The image is saved in oracle database in BLOB field.Now I want to show this image on my web form.I am using the code below but it's not working该图像保存在 BLOB 字段的 oracle 数据库中。现在我想在我的 Web 表单上显示此图像。我正在使用下面的代码,但它不起作用

C# CODE : C# 代码:

DataTable dt=new DataTable();
            dt=emppersonal.GetPhoto(Session["empcd"].ToString());
            byte[] barrImg = (byte[])dt.Rows[0]["photo"];
            string base64String = Convert.ToBase64String(barrImg);
            UserImage.ImageUrl = "data:image/jpeg;base64," + base64String; 

ASPX CODE: ASPX 代码:

   <asp:Image ID="UserImage"  runat="server" />

try this.尝试这个。 hope it helps希望能帮助到你

            DataTable dt = new DataTable();
            dt = emppersonal.GetPhoto(Session["empcd"].ToString());
            byte[] barrImg = (byte[])dt.Rows[0]["photo"];
            string base64String = Convert.ToBase64String(barrImg, 0, barrImg.Length);
            UserImage.ImageUrl = "data:image;base64," + base64String;

Create a ImageHandler.ashx file with the code as follows:创建一个 ImageHandler.ashx 文件,代码如下:

public void ProcessRequest (HttpContext context) {
    try
    {
        string empcd = context.Request.QueryString["empcd"].ToString();
        string conStr = WebConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ConnectionString;

        SqlConnection con = new SqlConnection(conStr);
        SqlCommand cmd = new SqlCommand("select photo from mytable where id = @empcd", con);
        cmd.Parameters.Add("@empcd", SqlDbType.Int).Value = empcd;
        con.Open();
        string data = cmd.ExecuteScalar().ToString();
        con.Close();
        cmd.Dispose();
        context.Response.BinaryWrite(Convert.FromBase64String(data));
    }
    catch (Exception ex)
    {
        //if some exceptions will occur
    }
}

public bool IsReusable {
    get {
        return false;
    }
}

And in your .aspx page:在您的 .aspx 页面中:

<asp:Image runat="server" ID="imgPhoto" ImageUrl="ImageHandler.ashx?empcd=some_value_here" />

You need to change a sql-query as you need.您需要根据需要更改 sql-query。

Hope this helps.希望这可以帮助。

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

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