图像保存在BLOB字段中的oracle数据库中。现在我想在我的网络表单上显示这个图像。我正在使用下面的代码,但它不能正常工作
C#CODE:
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代码:
<asp:Image ID="UserImage" runat="server" />
答案 0 :(得分:1)
试试这个。希望它有所帮助
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;
答案 1 :(得分:0)
使用以下代码创建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;
}
}
在您的.aspx页面中:
<asp:Image runat="server" ID="imgPhoto" ImageUrl="ImageHandler.ashx?empcd=some_value_here" />
您需要根据需要更改sql-query。
希望这有帮助。