日期:2014-05-18  浏览次数:20405 次

在asp.net中二进制图片显示?
<asp:TemplateField HeaderText="缩略图">
  <EditItemTemplate>
  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  </EditItemTemplate>
  <ItemTemplate>
  <a href="../showImg.aspx?object=AD&objectID=<%# Eval("ID") %>" target="_blank">
  <img onload="ResizeImage(this,60,60)" alt="图片" src="../showImg.aspx?object=AD&objectID=<%# Eval("ID") %>" /></a>
  </ItemTemplate>
  </asp:TemplateField>

这是gridview里的一模板列,要显示缩略图/

showImag.aspx是要显示图片的页面.

这个怎么做啊?是要从数据库的二进制图片读取出来!谢谢!

------解决方案--------------------
C# code

读取图片
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
public partial class Default2 : System.Web.UI.Page
{
    MySqlConnection conn = new MySqlConnection(System.Configuration.ConfigurationManager.AppSettings["conmy"]);
    protected void Page_Load(object sender, EventArgs e)
    {
      
        if (!this.Page.IsPostBack) 
        {
            this.show();  
        }
    }
    public void show() 
    {
        
            string ss = Request.QueryString["id"].ToString();
            string s3 = "select * from imgpic  where id=" + ss;
            MySqlCommand comm = new MySqlCommand(s3, conn);
            conn.Open();
            MySqlDataReader dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
            while(dr.Read())
            {
                Response.Clear();
                Response.C;
                Response.BinaryWrite((byte[])dr["myimg"]);//读取
            }
            Response.End();
            conn.Close();
    }
}
注意图片显示要在另一张页面中
在你要显示的页面添加img 控件src="显示你图片的页面"; 
122058662 最后编辑于 2008-04-22 09:59:24

------解决方案--------------------
在showImg.aspx中写代码

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

上面是引用
protected void Page_Load(object sender, EventArgs e)
{
// 在此处放置用户代码以初始化页面

连接数据库读取数据-----------------
SqlDataReader Dr = data.GetDataReader(sql);

if (Dr != null)
{
while (Dr.Read())
{
if (Dr[0] != null)
{
Response.ContentType = "image/jpeg";
Response.BinaryWrite((byte[])Dr["photo"]);
}
else
{
Response.Write("暂无图片");
}
}

}
else
{
Response.Write("暂无图片");
}
Dr.Close();
}