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

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. Android invalidate()自动清屏,屏幕刷新

    invalidate()是用来刷新View的,必须是在UI线程中进行工作.比如在修改某个view的显示时,调用invalidate()才能看到重新绘制的界面.invalidate()的调用是把之前的旧 ...

  2. js setTimeout深度递归后完成回调

    setTimout原型: iTimerID = window.setTimeout(vCode, iMilliSeconds [, sLanguage])     setTimeout有两种形式 se ...

  3. POJ 2586 Y2K Accounting Bug(贪心)

    题目连接:http://poj.org/problem?id=2586 题意:次(1-5.2-6.3-7.4-8.5-9.6-10.7-11.8-12),次统计的结果全部是亏空(盈利-亏空<0) ...

  4. php类 静态变量赋值 static $name="abc"

    <?php class test { static $name="abc"; } echo test::$name; 1.巴斯科范式 class_statement: var ...

  5. UVa 11054 Wine trading in Gergovia

    题意: 直线上有n个等距的酒庄,每个酒庄对酒的需求量为ai(正数说明需要买酒,负数需要卖酒),而且保证所有的酒庄供需平衡. 搬运x个单位的酒到相邻的酒庄需要x个劳动力,求要使所有酒庄供需平衡最少需要多 ...

  6. LCA与RMQ

    一.什么是LCA? LCA:Least Common Ancestors(最近公共祖先),对于一棵有根树T的任意两个节点u,v,求出LCA(T, u, v),即离跟最远的节点x,使得x同时是u和v的祖 ...

  7. (十二)学习CSS之box-sizing 属性

    参考:http://www.w3school.com.cn/cssref/pr_box-sizing.asp CSS3 box-sizing 属性 定义和用法 box-sizing 属性允许您以特定的 ...

  8. 在ubuntu on windows 上安装jekyll

    已知问题 安装ruby之后,重新启动ubuntu on windows,输入ruby命令,提示找不到. 发现的解决办法 执行下面命令 source /home/xxxx/.rvm/scripts/rv ...

  9. java泛型小总结

    一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: public class GenericTest { public static void main(String[] a ...

  10. 55人班37人进清华北大的金牌教师之32条教育建言! z

    他带的一个55人的班,37人考进清华.北大,10人进入剑桥大学.耶鲁大学.牛津大学等世界名校并获全额奖学金,其他考入复旦.南开等大学.不仅 如此,校足球冠军.校运动会总冠军.校网页设计大赛总冠军等6项 ...