我有个datagrid,每行里有个<image>,如下:
<ItemTemplate>
<asp:image id="imgPic" runat="server" ImageUrl='<%# FormatImage(DataBinder.Eval(Container.DataItem, "PicID")) %>'></asp:image>
</ItemTemplate>cs文件中有:
protected string FormatImage(object obj)
{
return "showPic.aspx?picid=" + obj.ToString();
}
==============================
showPic.aspx页面如下:
private void Page_Load(object sender, System.EventArgs e)
{
int picid = Request["picid"]==null?-1:Convert.ToInt32(Request["picid"]); if(picid <= -1)
{
Response.Write("没有图片");
}
else
{
DbTool tool = new DbTool ();//连接数据库 string sql = "select picture,pic_type,pic_size from ba_crm_pic where pic_id=" + picid;
SqlDataReader reader = tool.RunRdSql(sql); reader.Read();     
Response.ContentType = (string)reader["pic_type"];//设定输出文件类型
//输出图象文件二进制数制
Response.BinaryWrite((byte[])reader["picture"]);
Response.End();
reader.Close();
}
}
=============================
为什么我打开datagrid的页面,图片位置不显示图片
如果单独打开showPic.aspx?picid=1,则会出现“文件下载”的窗口,为什么呢?哪位高手能帮我看看?谢了啊

解决方案 »

  1.   

    你不能这么写
    你最好先生成一个图片文件 比如a.JPG
    然后在把数据库得图片写到a.JPG里面去
    再调用a.JPG
    ---------------------------
    苦海无边 回头无岸
    ---------------------------
      

  2.   

    这是打开图片的代码,你看看:
    在showimg.aspx中写
    this.Conn.Open();
    string sql="select * from newspic where id='"+imageid+"'";
    da=new SqlDataAdapter(sql,Conn);
    ds=new DataSet();
    da.Fill(ds,"newspic");
    if(ds.Tables[0].Rows.Count<=0)
    {
    Response.Write("");
    Response.End();
    return;
    }
    else
    {
    Session["pic"]=ds.Tables[0].Rows[0]["pic"];
    Session["pictype"]=ds.Tables[0].Rows[0]["pictype"];
    Response.Expires=0;
    Response.Buffer=true;
    Response.Clear();
    Response.ContentType=Session["pictype"].ToString();
    Response.BinaryWrite((byte[])Session["pic"]); 
    Session["pic"]="";
    Session["pictype"]="";
    }
    然后你在别的页面可以这样用
    <img src="showimg.aspx?id=<%=id%>">
      

  3.   

    哈哈,太好了,按照songxiaozhao(雨朋) 的指导改了一下,果然可以了,多谢二位