在ASP.NET中经常需要使用GridView的一列来显示图片,下面是在实践中使用到的方法,记下了,以后好参考:
第一种是添加ImageField列,然后设置DataImageUrlField和DataImageUrlFormatString显示,以下是实现方式
<asp:ImageField DataImageUrlField="CHM_RowID" DataImageUrlFormatString="CQU_CHM_ShowImage.aspx?chm_rowid={0}"
HeaderText="图片">
</asp:ImageField>后台CQU_CHM_ShowImage.aspx.cs文件中需要添加的代码是:
protected void Page_Load(object sender, EventArgs e)
{
int chm_rowid = Convert.ToInt32(Request.QueryString["chm_rowid"].ToString());
//string imageNo = Request.QueryString["imageNo"];
CHMPropertyBLL _CHMPropertyBLL = new CHMPropertyBLL();
DataTable dt = _CHMPropertyBLL.GetByPrimaryKey(chm_rowid);
MemoryStream stream = new MemoryStream();
byte[] image = (Byte[])dt.Rows[0]["CHM_Image1"];
stream.Write(image, 0, image.Length);
Bitmap bitmap = new Bitmap(stream);
Response.ContentType = "image/jpeg";
bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
stream.Close();
}第二种方式是增加模板列,此方法可以控制图片大小:
<asp:TemplateField HeaderText="图片预览">
<ItemTemplate>
<img src='CQU_CHM_ShowImage.aspx?chm_rowid=<%# Eval("CHM_RowID") %>' width="150px" height="100px" />
</ItemTemplate>
</asp:TemplateField>點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦