1 //浏览图片
  2
  3         private void btnUp_Click(object sender, EventArgs e)
  4
  5         {
  6
  7             OpenFileDialog ofd = new OpenFileDialog();
  8
  9             ofd.Title = "选择要上传的图片";
 10
 11             ofd.Filter = "All Files(*.*)|*.*|位图(*.bmp)|*.bmp|JPEG(*.jpg)|*.jpg";
 12
 13             ofd.ShowDialog();
 14
 15             textBox1.Text = ofd.FileName;
 16
 17             if (!File.Exists(ofd.FileName))
 18
 19             {
 20
 21                 MessageBox.Show("照片为空");
 22
 23                 return;
 24
 25             }
 26
 27         }
 28
 29
 30
 31
 32
 33         //上传保存到数据库
 34
 35         private void btnUpLoad_Click(object sender, EventArgs e)
 36
 37         {
 38
 39             string strPath = txtbImage.Text.Trim();
 40
 41             FileStream fs = new FileStream(strPath, FileMode.Open, FileAccess.Read);
 42
 43             byte[] byteFile = new byte[fs.Length];
 44
 45             fs.Read(byteFile, 0, (int)fs.Length);
 46
 47             fs.Close();
 48
 49             SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True");
 50
 51                 try
 52
 53                 {
 54
 55                     SqlCommand cmd = new SqlCommand();
 56
 57                     cmd.Connection = conn;
 58
 59
 60
 61                     string strSql = "insert into test(FileName,Img) Values(@FileName,@Img)";
 62
 63                     cmd.CommandText =strSql ;
 64
 65                     //cmd.Parameters.AddWithValue("@FileName", strPath);
 66
 67                     //cmd.Parameters.AddWithValue("@Img", byteFile);
 68
 69                     //或者
 70
 71                     SqlParameter[] parameters = new SqlParameter[2];
 72
 73                     parameters[0] = new SqlParameter("@FileName", SqlDbType.NVarChar, 200);
 74
 75                     parameters[0].Value = strPath;
 76
 77                     parameters[1] = new SqlParameter("@Img", SqlDbType.Image,int.MaxValue);
 78
 79                     parameters[1].Value = byteFile;
 80
 81                     cmd.Parameters.AddRange(parameters);
 82
 83                     conn.Open();
 84
 85                     cmd.ExecuteNonQuery();
 86
 87                     conn.Close();
 88
 89                     MessageBox.Show("上传成功");
 90
 91                 }
 92
 93                 catch
 94
 95                 {
 96
 97                     conn.Close();
 98
 99                     MessageBox.Show("上传失败!");
100
101                 }
102
103         }
104
105 从数据库读取图片显示到窗体:
106 1
107 2
108 3
109 4
110 5
111 6
112 7
113 8
114 9
115 10
116 11
117 12
118 13
119 14
120 15
121 16
122 17
123 18
124 19
125 20
126 21
127 22
128 23
129 24
130 25
131 26
132 27
133 28
134 29
135 30
136 31
137 32
138 33
139 34
140 35
141 36
142 37
143 38
144 39
145 40
146 41
147 42
148 43
149 44
150 45
151 46
152 47
153 48
154 49
155 50
156 51
157 52
158 53
159 54
160 55
161 56
162 57
163 58
164 59
165 60
166 61
167 62
168 63
169 64
170 65
171 66
172 67
173 68
174 69
175 70
176 71
177 72
178 73
179 //读到图片显示到PictureBox
180
181         private void btnDownLoad_Click(object sender, EventArgs e)
182
183         {
184
185             byte[] bytFile;
186
187             SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True");
188
189                 try
190
191                 {
192
193                     SqlCommand cmd = new SqlCommand();
194
195                     string strSql = "select img from test where ID=3";
196
197                     cmd.Connection = conn;
198
199                     cmd.CommandText = strSql;
200
201                     conn.Open();
202
203                     SqlDataReader sdr = cmd.ExecuteReader();
204
205                     if (sdr.Read())
206
207                     {
208
209                         bytFile = (Byte[])sdr["Img"];
210
211                     }
212
213                     else
214
215                     {
216
217                         bytFile = new byte[0];
218
219                     }
220
221                     sdr.Close();
222
223                     conn.Close();
224
225                     //通过内存流MemoryStream,
226
227                     //把byte[]数组fileContent加载到Image中并赋值给图片框的Image属性,
228
229                     //让数据库中的图片直接显示在窗体上。
230
231                     MemoryStream ms = new MemoryStream(bytFile, 0, bytFile.Length);
232
233                     this.picImage.Image = Image.FromStream(ms);
234
235                     //关闭内存流
236
237                     ms.Close();
238
239                 }
240
241                 catch
242
243                 {
244
245                     conn.Close();
246
247                     MessageBox.Show("失败");
248
249                 }
250
251         }

代码转自IT学习广场http://www.itxxgc.com/net/detail/30

                 来自凌波小屋-----冯和超的笔记-------

转载于:https://www.cnblogs.com/lingbohome/p/4655043.html

C#(WinForm)上传图片保存到数据库和从数据库读取图片显示到窗体相关推荐

  1. matlab怎么输出图像文件夹,Matlab读取图片 显示和保存图像的相关操作

    当前有部份朋友还不清楚Matlab读取图片 显示和保存图像的操作,所以下面绿软吧就带来Matlab读取图片 显示和保存图像的相关操作,一起来看看吧! Matlab读取图片 显示和保存图像的相关操作 打 ...

  2. WinForm应用程序框架设计之WinAction(一:显示列表窗体)

    我们来先看看WinAction的显示列表窗体的流程: 具体代码参考:         public void InitListForm(Form listForm, object entity)    ...

  3. asp.net mvc 从数据库中读取图片的实现代码

    首先是创建一个类,继承于ActionResult,记住要引用System.Web.Mvc命名空间,如下: public class ImageResult : ActionResult { publi ...

  4. php图片保存在mysql_php实现上传图片保存到数据库的方法

    php实现上传图片保存到数据库的方法.分享给大家供大家参考.具体分析如下: php 上传图片,一般都使用move_uploaded_file方法保存在服务器上.但如果一个网站有多台服务器,就需要把图片 ...

  5. php实现上传图片保存到数据库的方法

    http://www.jb51.net/article/61034.htm 作者:傲雪星枫 字体:[增加 减小] 类型:转载 这篇文章主要介绍了php实现上传图片保存到数据库的方法,可通过将图片保存在 ...

  6. JavaWeb上传图片到服务器,存储到数据库,并在页面显示

    JavaWeb上传图片到服务器,存储到数据库,并在页面显示 Servlet @Overrideprotected void doPost(HttpServletRequest req, HttpSer ...

  7. 基于Enterprise Library的Winform开发框架实现支持国产达梦数据库的扩展操作

    由于一个客户朋友的需求,需要我的Winform开发框架支持国产达梦数据库的操作,这个数据库很早就听过,但是真正一般项目用的很少,一般在一些特殊的项目可能需要用到.由于我的Winform开发框架,是基于 ...

  8. 【WPF】如何保存RichTextBox的文本到数据库?以及如何对RichTextBox的Document做绑定?...

    这几天一直有人问我如何保存RichTextBox的文本到数据库,包括格式等等,然后需要的再从数据库取出来,并且显示到RichTextBox中. 其实,RichTextBox的文本是一个FlowDocu ...

  9. 图片保存到数据库以及从数据库中Load图片

    图片保存到数据库以及从数据库中Load图片 假设有一个数据库Test,下面创建一个表,它有一个字段是image类型,我们把图片转换成Byte数组存放在数据库中: CREATE TABLE Images ...

最新文章

  1. 还是畅通工程(1233 并查集+kruskal)
  2. PriorityQueue源码解析
  3. 最新最全vuepress零基础搭建(github搭建+新增插件)
  4. java 创建web项目_java – Eclipse:以编程方式创建动态Web项目
  5. 【OpenCV】OpenCV函数精讲之 -- 通道合并:merge()函数
  6. 【BZOJ2120】数颜色,带修莫队
  7. 有趣的算法(六):3分钟看懂插入排序(C语言实现)
  8. 西门子电机选型参考一
  9. echarts使用之坑 隐藏显示echarts变形 echarts官网访问不了
  10. 一个前端面试官的自白:Connecting the Dots
  11. Java学习-Thread
  12. 标题一定要长~~~~长~~~~~~~~~~~~~~长~~~~~~~~
  13. 序列相似性比较与同源性分析
  14. 各种存储硬件(Memery)区分(ROM、RAM、DRAM、SRAM和FLASH)
  15. AD PCB布板提示The following exception occurred whilst loading section primitive paramenters...解决方法
  16. 松软科技web课堂:SQLServer之UCASE() 函数
  17. 华为“More Bits, Less Watts”新践行
  18. 3090人工神经网络工作站配置
  19. KANKAN AI不良信息过滤技术:用数据证明自己是最好的
  20. linux系统中怎么截取某一天的日志,Linux系统如何截取线上日志

热门文章

  1. 驱动级模拟驱动级模拟:直接读写键盘的硬件端口!
  2. java利用正则截取字符串中的数字
  3. Android之Inflate()方法用途+setContentView和inflate区别
  4. 微软企业库5.0 学习之路——UnityPIAB 通过配置实现AOP
  5. FCKeditor图片上传 进度条不动
  6. 关于OpenMesh在Vs2008下编译与安装
  7. 你若安好,便是晴天。
  8. 数据访问函数库 for ado.net2.0
  9. Netty+SpringBoot+FastDFS+Html5实现聊天App详解(一)
  10. 三款运用二维码分享与预览的原型设计工具