GridView显示图片

参考文章:http://www.cnblogs.com/jll/archive/2005/12/14/297153.html

以下内容出处忘了 = =!

在ASP.NET中经常需要使用GridView的一列来显示图片,下面是在实践中使用到的方法:

第一种:

添加ImageField列,然后设置DataImageUrlField和DataImageUrlFormatString显示,以下是实现方式

<asp:ImageField DataImageUrlField="CHM_RowID" DataImageUrlFormatString="CQU_CHM_ShowImage.aspx?chm_rowid={0}" HeaderText="图片">

</asp:ImageField>

第二种:

增加模板列,此方法可以控制图片大小

<asp:TemplateField HeaderText="图片预览"><ItemTemplate>

  <img src='CQU_CHM_ShowImage.aspx?chm_rowid=<%# Eval("CHM_RowID") %>' width="150px" height="100px" />

</ItemTemplate></asp:TemplateField>

显示图片的页的代码:

CQU_CHM_ShowImage.aspx.cs:

protected void Page_Load(object sender, EventArgs e)

    {

   //读取数据

        int chm_rowid = Convert.ToInt32(Request.QueryString["chm_rowid"].ToString());

        CHMPropertyBLL _CHMPropertyBLL = new CHMPropertyBLL();

        DataTable dt = _CHMPropertyBLL.GetByPrimaryKey(chm_rowid); 

        byte[] image = (Byte[])dt.Rows[0]["CHM_Image1"];  //第一行的CHM_Image1字段

  //读取数据的另一种方法(只读取图片):

   //SqlCommand cmd = new SqlCommand("select CHM_Image1 from CHMTable where chm_rowid=" + chm_rowid, con);
        //byte[] image = (byte[])cmd.ExecuteScalar();

  //显示图片

    MemoryStream stream = new MemoryStream();

        stream.Write(image, 0, image.Length);

        Bitmap bitmap = new Bitmap(stream);

        Response.ContentType = "image/jpeg";

        bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);

        stream.Close();

  //显示图片的另一种方法:

   //Response.BinaryWrite(image);
        //Response.End();

  } 

原文地址:https://www.cnblogs.com/vipcjob/p/1589766.html