欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

gridview 显示图片的实例代码

程序员文章站 2023-12-15 09:13:04
1.将图片以二进制存入数据库 2.读取二进制图片在页面显示 3.设置image控件显示从数据库中读出的二进制图片 4.gridview中imagefield以url方...

1.将图片以二进制存入数据库

2.读取二进制图片在页面显示

3.设置image控件显示从数据库中读出的二进制图片

4.gridview中imagefield以url方式显示图片

5.gridview显示读出的二进制图片

====================

1.将图片以二进制存入数据库

复制代码 代码如下:

//保存图片到数据库

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 = http://www.cnblogs.com/wycoo/archive/2012/02/07/photo;

   myconn.open();

   mycomm.executenonquery();

   myconn.close();

}

2.读取二进制图片在页面显示

复制代码 代码如下:

//读取图片

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控件显示从数据库中读出的二进制图片
复制代码 代码如下:

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方式显示图片

----------------------------

<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显示读出的二进制图片

复制代码 代码如下:

//样板列
<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;

   }
}

上一篇:

下一篇: