日期:2014-05-17  浏览次数:20516 次

从数据库读取二进制数据问题,做课程设计遇到的
Image.imageUrl='<% #Bind("Url")%>' 
Url是Image类型的
后台已经找到数据源

------解决方案--------------------
public System.Drawing.Image CreateImageBitArray(BitArray[] bitArr)
{

}
试试这个方法
------解决方案--------------------
Image.imageUrl='<% #Bind("Url")%>'
这样写是不行的,你需要
Image.ImageUrl='<%# "showImage.aspx?id=" + Eval("ID")%>'

showImage.aspx里面写
<%@ Page Language="C#" %>

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
int id = 0;
if (Int32.TryParse(Request.QueryString["id"], out id))
{
string strCnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Image2Access.mdb";
System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection(strCnString);
String strSql = "select PersonImageType,PersonImage from Person Where PersonId=@id";
System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand(strSql, myConnection);
command.Parameters.AddWithValue("@id", id);
//打开连接,执行查询
myConnection.Open();
System.Data.OleDb.OleDbDataReader dr = command.ExecuteReader();
if (dr.Read())
{
byte[] image = (byte[])dr["PersonImage"];
Response.ClearContent();
Response.ContentType = dr["PersonImageType"].ToString();
Response.BinaryWrite(image);
Response.End();
}
else
{
OutPutNonePicture();
}
dr.Close();
myConnection.Close();
myConnection.Dispose();
}
else
{
OutPutNonePicture();
}
}

/// <summary>
/// 对于不存在的图片,输出none.gif
/// </summary>
private void OutPutNonePicture()
{
Response.ClearContent();
Response.ContentType = "image/gif";
Response.WriteFile(Server.MapPath("~/none.gif"));
Response.End();
}
</script>

------解决方案--------------------
孟老师说的对。
------解决方案--------------------
参考:
http://www.cnblogs.com/insus/articles/1430434.html