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对象模型以动态设 ...
随机推荐
- MySQL数据库服务器安装标准
MySQL数据库服务器安装标准 (1).BIOS优化,阵列配置 1.1:关闭CPU节能,因为服务器品牌众多,BIOS设置不相同,主要是关闭CPU节能,如C1,DELLR730,已经智能设置,直接有个p ...
- Oracle数据字典详解
学习笔记:oracle数据字典详解 --- 本文为TTT学习笔记,首先介绍数据字典及查看方法,然后分类总结各类数据字典的表和视图.然后列出一些附例. 数据字典系统表,保存在system表空间中. ...
- Hadoop安装教程_单机/伪分布式配置_Hadoop2.6.0/Ubuntu14.04
摘自: http://www.cnblogs.com/kinglau/p/3796164.html http://www.powerxing.com/install-hadoop/ 当开始着手实践 H ...
- hdu4638Group
http://acm.hdu.edu.cn/showproblem.php?pid=4638 求某一区间所包含的连续的段 对于乱序的数 到了i这个数所包含的段数 首先把这个数看作单独的段 再看一下前面 ...
- poj 2531 Network Saboteur( dfs )
题目:http://poj.org/problem?id=2531 题意:一个矩阵,分成两个集合,求最大的 阻碍量 改的 一位大神的代码,比较简洁 #include<stdio.h> #i ...
- bzoj1433:[ZJOI2009]假期的宿舍
明显的二分图最大匹配. #include<cstdio> #include<cstring> #include<cctype> #include<algori ...
- Java [leetcode 13] Roman to Integer
问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- jquery通过ajax获取数据(优化、封装)
下载页面查看: makeGrid.js ,column.js ,XiangMuGuanLi.aspx <div class="tb_container" id=&quo ...
- spring的三种注入方式
接口注入(不推荐) 构造器注入(死的应用) getter,setter方式注入(比较常用) Type1 接口注入 我们常常借助接口来将调用者与实现者分离.如: public class ClassA ...
- JSP文件上传代码
一.首先建立一个上传的界面,取名为a.jsp,代码如下 <%@ page contentType="text/html; charset=utf-8" language=&q ...