与图片的二进制数据库存储和显示

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控件显示图片的更多相关文章

  1. 让DELPHI自带的richedit控件显示图片

    让DELPHI自带的richedit控件显示图片 unit RichEx; { 2005-03-04 LiChengbin Added: Insert bitmap or gif into RichE ...

  2. Android:ImageView控件显示图片

    1)android显示图片可以使用imageView来呈现,而且也可以通过ImageButton来实现给button添加图片. 2)在创建一个ImageView后,显示图片绑定元素是:android: ...

  3. VC2005中将Picture控件显示图片保存为BMP,JPG等格式

    1.在stdafx.h头文件中加入 #include <atlimage.h> 2.保存图片 方法一:   HBITMAP hBitmap = NULL; //创建位图段 BITMAPIN ...

  4. winform下picturebox控件显示图片问题

    viewData_pictureBox.SizeMode=PictureBoxSizeMode.StretchImage;图片会自动按照比例缩放来完全显示在你的PictureBox中.

  5. Repeater, DataList, 和GridView控件的区别

    http://blog.sina.com.cn/s/blog_646dc75c0100h5p6.html http://www.cnblogs.com/phone/archive/2010/09/15 ...

  6. 数据绑定技术一:GridView控件

    在网站或应用程序中,要显示数据信息,可用到ASP.NET提供的数据源控件和能够显示数据的控件. 一.数据源控件 数据源控件用于连接数据源.从数据源中读取数据以及把数据写入数据源. 1.数据源控件特点 ...

  7. 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常

    代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常 设置 padding=0

  8. asp.net GridView控件的列属性

    BoundField 默认的数据绑定类型,通常用于显示普通文本 CheckBoxField 显示布尔类型的数据.绑定数据为TRUE时,复选框数据绑定列为选中状态:绑定数据为FALSE时,则显示未选中状 ...

  9. 027. asp.net中数据绑定控件之 GridView控件

    GridView控件支持下面的功能: 绑定至数据源控件, 如SqlDataSource 内置排序功能 内置更新和删除功能 内置分页功能 内置行选择功能 可以编程方式访问GridView对象模型以动态设 ...

随机推荐

  1. C#和.net之间的关系

    What is the difference between C# and .NET? In addition to what Andrew said, it is worth noting that ...

  2. Android开发之IP拨号器原理

    IP拨号器,使用了Android的广播接收者(BroadCastReceiver),在广播中把已保存的ip号码放在拨打电话号码的前面(getResultData()),然后把修改后的号码设置到广播中( ...

  3. Java版本的删除指定目录及子目录下名叫“xxx.txt”的所有文件

    以前写过一个python版本的,但是在查找文件路径的时候出现错误,无法正确的获取到文件的路径,就造成无法删除该路径下的“xxx.txt”文件. 当时以为是windows版本系统的错误造成这个问题的,也 ...

  4. Windows 7/8 创建WIFI热点

    问题描述:很多人(特别是中国的大学生)都拥有一台联网的笔记本电脑,而手机使用的却是电信运营商提供的限制数量和速度的GPRS. 很多人不敢想象:联网的笔记本电脑能够将其流量以WiFi的形式共享出来供其它 ...

  5. 什么是WebService

    举个例子:现在有5个项目,项目彼此独立,甚至都不是同一类语言进行开发的.这5个项目是:百度知道,百度贴吧,百度新闻,百度视频,百度百科.突然有一天,老板说:把这几个系统揉称一个大项目,起名直接叫做百度 ...

  6. UVA 1515 Pool construction 水塘(最大流,经典)

    题意: 给一个h*w的矩阵,每个格子中是'#'和'.'两个符号之一,分别代表草和洞.现在要将洞给围起来(将草和洞分离),每条边需花费b元(即将一个洞包起来需要4边,将2个连续的洞包起来需要6边,省了2 ...

  7. 学习面试题Day03

    1.Java中的注释有哪些? 如果不算Annotation,Java的注释有3种,即行注释.块注释和文档注释.它们往往适合于不同地方的注释,其中文档注释比较特殊,它的注释信息可以进入到javadoc文 ...

  8. iOS开发之UITextField

    UITextField基本知识 UITextField展示的是一些可编辑的内容,并且与用户有一些交互.比如当你在虚拟键盘上按下return键时,一般会关联到键盘隐藏事件上.UITextField的一些 ...

  9. MVC Action Filter

    ASP.NET MVC Framework支持四种不同类型的Filter: Authorization filters – 实现IAuthorizationFilter接口的属性. Action fi ...

  10. mkimage工具 加载地址和入口地址 内核启动分析

    第三章第二节 mkimage工具制作Linux内核的压缩镜像文件,需要使用到mkimage工具.mkimage这个工具位于u-boot-2013. 04中的tools目录下,它可以用来制作不压缩或者压 ...