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 = 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;
    }   
}

转载于:https://www.cnblogs.com/freeliver54/archive/2007/05/23/756652.html

GridView控件 Image控件 与图片的二进制数据库存储和显示相关推荐

  1. 用C#实现图片数据库存储与显示

    在网络上查找了一些资料,对C#实现图片的数据库存储与显示作个总结!所有代码都可以直接复制运行.准备数据库,在SQL2000数据库的pubs数据库中建立一个ImageStore数据表,字段有ImgID, ...

  2. C#中RichEdit控件,保存文本和图片到mysql数据库

    分别通过内存流和RTF文件保存,个人感觉是较为完善的两种方法,希望大家可以用得到,有什么技术分享,欢迎下面留言 方法1: //建立内存流 MemoryStream ms = new MemoryStr ...

  3. 数据库存储图片解决方案

    商品图片,用户上传的头像,其他方面的图片.目前业界存储图片有两种做法: 1. 把图片直接以二进制形式存储在数据库中 一般数据库提供一个二进制字段来存储二进制数据.比如mysql中有个blob字段.or ...

  4. Python存取图片至服务器数据库中

    前言 由于项目需求,需要将识别的人脸图片存储到服务器的数据库中,经过了解得知目前业界存储图片主要有两种方式: 图片存储在磁盘上,即服务器文件系统中,数据库字段中保存的是图片的路径. 图片以二进制形式直 ...

  5. Gridview导出到Excel,Gridview中的各类控件,Gridview中删除记录的处理

    Asp.net 2.0中新增的gridview控件,是十分强大的数据展示控件,在前面的系列文章里,分别展示了其中很多的基本用法和技巧(详见< ASP.NET 2.0中Gridview控件高级技巧 ...

  6. 在GridView内访问特定控件

    本文我将为你演示如何访问GridView中的特定控件.我们会看到怎样去访问TextBox控件,DropDownList控件以及ListBox控件. 添加控件到GridView: 你可以简单地使用 &l ...

  7. 【 iOS 应用开发 】 UIKit 控件 ( 代码生成控件 | UIView 属性方法 | Storyboard | Bundle | Property List | 动画 | 图片内存优化 )

    文章目录 一. 代码生成控件 1. 创建设置项目 ( 1 ) 创建项目 ( ① 选择 Create a new Xcode project | ② 创建 Single View Application ...

  8. FileUpload控件实现单按钮图片自动上传并带预览显示

    FileUpload控件实现单按钮图片自动上传并带预览显示 1.实现原理: FileUpload控件默认不支持服务端的ONCHANGE事件,此时用一种变通的方法借用客户端的onchange事件,调用_ ...

  9. 0811-按钮操作(加法计算器)(拖控件找控件代码属性名称)(frame center bounds)(上下左右移动button图片)...

    -------------------- 加法计算器 实现步骤 1.拖控件   改textField键盘属性为numberPad ,label双击修改名称自动缩小尺寸  改属性名称不会改尺寸,   放 ...

最新文章

  1. Golang学习-基础命令
  2. Oracle数据库管理----性能优化
  3. 爬虫,如何防止被ban之策略大集合
  4. window.event.returnValue=false
  5. Glib 对 C 函数进行单元测试
  6. 凭证 金蝶_金蝶软件账务处理流程之——凭证录入
  7. 你了解SVN, CVS等版本控制器吗?
  8. 【Linux开发】Ubuntu下几个软件的配置记录backup
  9. 遇到的retain cycle例子
  10. MySQL--mysqldump的权限说明
  11. “Win10系统更新后,插上主机耳机孔,没有声音 / 扬声器未插入”解决方法
  12. UE4开发游戏的流程
  13. android 动态库符号表,Android NDK隐藏jni动态库的内部符号表
  14. OpenLayers实战(四)控制图标显示隐藏
  15. “马”道微信:全面拆解微信营销模式
  16. 家里用服务器放在哪个位置,家用路由器放在什么位置比较合理?
  17. Python打包工具
  18. 64位Slitaz Linux下Glibc 2.20编译式更新安装成功
  19. 计算机作文范文,未来计算机作文范文.docx
  20. SPM 超级位置模型

热门文章

  1. 简化电脑操作,不让多余操作浪费你的生命
  2. 百度接口根据关键字生成文章
  3. 博弈论系列—海盗分金币
  4. 大数相减(解析国密sm2_bn_sub算法与自定义算法)
  5. git-cz git commit 定制提交规范
  6. JAVA:实现一个CircularQueue循环队列算法(附完整源码)
  7. Win7 运行bat批处理文件时怎么隐藏cmd命令提示符窗口
  8. 风格迁移1-00:Liquid Warping GAN(Impersonator)-目录-史上最新无死角讲解
  9. 9.FLINK Sink\API\自定义sink
  10. Sentence Centrality Revisited for Unsupervised Summarization