GridView控件显示图片
与图片的二进制数据库存储和显示
1.将图片以二进制存入数据库
2.读取二进制图片在页面显示
3.设置Image控件显示从数据库中读出的二进制图片
4.GridView中ImageField以URL方式显示图片
5.GridView显示读出的二进制图片
====================
1.将图片以二进制存入数据库
Code//保存图片到数据库 protected void Button1_Click(object sender, EventArgs e) { //图片路径 string strPath = "~/photo/03.JPG"; string strPhotoPath = Server.MapPath(strPath); //读取图片 FileStream fs = new System.IO.FileStream(strPhotoPath, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); byte[] photo = br.ReadBytes((int)fs.Length); br.Close(); fs.Close(); //存入 SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa"); string strComm = " INSERT INTO personPhoto(personName, personPhotoPath, personPhoto) "; strComm += " VALUES('wangwu', '" + strPath + "', @photoBinary )"; SqlCommand myComm = new SqlCommand(strComm, myConn); myComm.Parameters.Add("@photoBinary", SqlDbType.Binary,photo.Length); myComm.Parameters["@photoBinary"].Value = photo; myConn.Open(); myComm.ExecuteNonQuery(); myConn.Close(); }
2.读取二进制图片在页面显示
Code
//读取图片 SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa"); string strComm = " SELECT personPhoto FROM personPhoto WHERE personName='wangwu' "; SqlCommand myComm = new SqlCommand(strComm, myConn); myConn.Open(); SqlDataReader dr = myComm.ExecuteReader(); while (dr.Read()) { byte[] photo = (byte[])dr["personPhoto"]; this.Response.BinaryWrite(photo); } dr.Close(); myConn.Close(); //或
///////////////////////////////////////////
SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa"); SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ", myConn); DataSet myds = new DataSet(); myConn.Open(); myda.Fill(myds); myConn.Close(); byte[] photo = (byte[])myds.Tables[0].Rows[0]["personPhoto"]; this.Response.BinaryWrite(photo);
3.设置Image控件显示从数据库中读出的二进制图片
Code
SqlConnection myConn = new SqlConnection("Data Source=192.168.0.36;Initial Catalog=TestDB;User ID=sa;Password=sa"); SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ", myConn); DataSet myds = new DataSet(); myConn.Open(); myda.Fill(myds); myConn.Close(); byte[] photo = (byte[])myds.Tables[0].Rows[0]["personPhoto"]; //图片路径 string strPath = "~/photo/wangwu.JPG"; string strPhotoPath = Server.MapPath(strPath); //保存图片文件 BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate)); bw.Write(photo); bw.Close(); //显示图片 this.Image1.ImageUrl = strPath;
4.GridView中ImageField以URL方式显示图片
Code ---------------------------- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="personName" HeaderText="姓名" /> <asp:ImageField DataImageUrlField="personPhotoPath" HeaderText="图片"> </asp:ImageField> </Columns> </asp:GridView> //后台直接绑定即可
5.GridView显示读出的二进制图片
Code //样板列 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"> <Columns> <asp:BoundField DataField="personName" HeaderText="姓名" /> <asp:ImageField DataImageUrlField="personPhotoPath" HeaderText="图片"> </asp:ImageField> <asp:TemplateField HeaderText="图片"> <ItemTemplate> <asp:Image ID="Image1" runat="server" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowIndex < 0) return; // System.ComponentModel.Container string strPersonName = (string)DataBinder.Eval(e.Row.DataItem, "personName"); Image tmp_Image = (Image)e.Row.Cells[2].FindControl("Image1"); if (!System.Convert.IsDBNull(DataBinder.Eval(e.Row.DataItem, "personPhoto"))) { // byte[] photo = (byte[])DataBinder.Eval(e.Row.DataItem, "personPhoto"); //图片路径 string strPath = "~/photo/" + strPersonName.Trim() + ".JPG"; string strPhotoPath = Server.MapPath(strPath); //保存图片文件 BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath, FileMode.OpenOrCreate)); bw.Write(photo); bw.Close(); //显示图片 tmp_Image.ImageUrl = strPath; } }
GridView控件显示图片的更多相关文章
- 让DELPHI自带的richedit控件显示图片
让DELPHI自带的richedit控件显示图片 unit RichEx; { 2005-03-04 LiChengbin Added: Insert bitmap or gif into RichE ...
- Android:ImageView控件显示图片
1)android显示图片可以使用imageView来呈现,而且也可以通过ImageButton来实现给button添加图片. 2)在创建一个ImageView后,显示图片绑定元素是:android: ...
- VC2005中将Picture控件显示图片保存为BMP,JPG等格式
1.在stdafx.h头文件中加入 #include <atlimage.h> 2.保存图片 方法一: HBITMAP hBitmap = NULL; //创建位图段 BITMAPIN ...
- winform下picturebox控件显示图片问题
viewData_pictureBox.SizeMode=PictureBoxSizeMode.StretchImage;图片会自动按照比例缩放来完全显示在你的PictureBox中.
- Repeater, DataList, 和GridView控件的区别
http://blog.sina.com.cn/s/blog_646dc75c0100h5p6.html http://www.cnblogs.com/phone/archive/2010/09/15 ...
- 数据绑定技术一:GridView控件
在网站或应用程序中,要显示数据信息,可用到ASP.NET提供的数据源控件和能够显示数据的控件. 一.数据源控件 数据源控件用于连接数据源.从数据源中读取数据以及把数据写入数据源. 1.数据源控件特点 ...
- 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常
代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常 设置 padding=0
- asp.net GridView控件的列属性
BoundField 默认的数据绑定类型,通常用于显示普通文本 CheckBoxField 显示布尔类型的数据.绑定数据为TRUE时,复选框数据绑定列为选中状态:绑定数据为FALSE时,则显示未选中状 ...
- 027. asp.net中数据绑定控件之 GridView控件
GridView控件支持下面的功能: 绑定至数据源控件, 如SqlDataSource 内置排序功能 内置更新和删除功能 内置分页功能 内置行选择功能 可以编程方式访问GridView对象模型以动态设 ...
随机推荐
- update多表陷阱
今天同学发了个sql题目 A1表 B1表 id num id snum 1 10 1 90 2 2000 3 4000 3 30 B表的数据插入A表当中 最后的结果 A表 1 90 2 2000 3 ...
- 转:JS日期加减,日期运算
原文 出处http://hi.baidu.com/tonlywang/item/685fba8933a2a756e73d1950 一.日期减去天数等于第二个日期 function cc(dd,dadd ...
- 【ZOJ】2112 Dynamic Rankings
树状数组套主席树模板题目. /* 2112 */ #include <iostream> #include <sstream> #include <string> ...
- 详解js中的寄生组合式继承
寄生组合式继承是js中最理想的继承方式, 最大限度的节省了内存空间. js中的寄生组合式继承要求是: 1.子对象有父对象属性的副本, 且这些不应该保存在子对象的prototype上. 2. ...
- poj 1125 Stockbroker Grapevine(最短路 简单 floyd)
题目:http://poj.org/problem?id=1125 题意:给出一个社交网络,每个人有几个别人可以传播谣言,传播谣言需要时间.问要使得谣言传播的最快,应该从那个人开始传播谣言以及使得所有 ...
- bzoj3157 3516
太神了,被数学题虐了 orz http://m.blog.csdn.net/blog/skywalkert/43970331 这道题关键是抓住m较小的特点,构造递推解决 ; ..,..] of lon ...
- jqGrid如何实现单选。
设置如下:multiselect:true // multi-select checkboxes appear multiboxonly:true // checkboxes act like rad ...
- 使用BusyBox制作Linux根文件系统
STEP 1:构建目录结构 创建根文件系统目录,主要包括以下目录/dev /etc /lib /usr /var /proc /tmp /home /root /mnt /bin /sbin ...
- HDU 4393 Throw nails
Throw nails Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Uva11732(trie)
题意:给你n个字符串 用strcmp()两两比较 ,求字符比较的总次数 分析: 数据量很大我们考虑用孩子兄弟表示法来表示字典树 #include <cstdio> #include < ...