C#从数据库读取图片并保存的两种方法
作者:风云SH变幻 时间:2024-01-12 14:39:22
方式一:
数据库用的是SQL 2008,数据表中存放的是图片的二进制数据,现在把图片以一种图片格式(如.jpg)导出,然后存放于指定的文件夹中,实现方式如下:
byte[] bytImg = (byte[])myDAL.DbHelperSQL.Query("SELECT F_Photo FROM myTable WHERE ID=1").Tables[0].Rows[0][0];
if (bytImg != null)
{
MemoryStream ms = new MemoryStream(bytImg);
Image img = Image.FromStream(ms);
img.Save("D:\\me.jpg");
}
方式二:
是windowform程序,数据库已经建好,图像以二进制形式存放在数据库的image表中,我想把符合查询条件的图像(大量)从数据库中读出,显示在form窗体上的一个控件(listview或imagelist还是picturebox?这个不知道那个合适),并保存到选择(或新建)的一个文件夹中
SqlDataAdapter da = new SqlDataAdapter("select * from newpicture", conn);//数据库连接,修改一下数据库的操作。
DataSet ds = new DataSet();
da.Fill(ds, "pic");//将符合条件的选项保存在数据集的pic表里
string picdotname;
string picfilename;
int piclength;
int i;
//添加新列
DataColumn newcolumn = ds.Tables["pic"].Columns.Add("pic_url", typeof(string));//给pic表添加新的一列pic_url,保存你的新写出的图片路径
for (i = 0; i < Convert.ToInt16(ds.Tables["pic"].Rows.Count); i++)
{
picdotname = ds.Tables["pic"].Rows[i]["pic_dot"].ToString();//图片的拓展名,你数据库要有这一列,如jpg
piclength = Convert.ToInt32(ds.Tables["pic"].Rows[i]["pic_length"]);//数据流的长度
picfilename = Server.MapPath("新建的文件夹名/") + "添加图片名"+ "." + picdotname;
FileStream fs = new FileStream(picfilename, FileMode.Create, FileAccess.Write);
byte[] piccontent = new byte[piclength];
piccontent = (byte[])ds.Tables["pic"].Rows[i]["pic_content"];
fs.Write(piccontent, 0, piclength);
fs.Close();//读出数据流写成图片
//最后把表绑定到控件上。
ds.Tables["pic"].Rows[i]["pic_url"] = "temp/temp" + i.ToString() + "." + picdotname;//意思给表pic的第i行,pic_url列里添加文件的路径值。
}
//数据源 = ds.Tables["pic"];//数据绑定
大体是这样吧,里面表名列名很多细节你按你的表修改吧!
来源:https://www.cnblogs.com/wzhk009/p/14202703.html
标签:c#,数据库,读取,图片,保存
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python简单的GUI程序示例详解
2021-04-10 20:17:16
![](https://img.aspxhome.com/file/2023/0/93590_0s.png)
Python如何使用队列方式实现多线程爬虫
2022-03-24 08:56:51
简单谈谈JS中的正则表达式
2023-07-22 00:50:22
![](https://img.aspxhome.com/file/2023/3/55963_0s.png)
python自动查询12306余票并发送邮箱提醒脚本
2023-08-26 09:34:37
![](https://img.aspxhome.com/file/2023/6/75166_0s.jpg)
Python Pandas实现数据分组求平均值并填充nan的示例
2021-03-24 11:51:30
![](https://img.aspxhome.com/file/2023/9/117629_0s.jpg)
Python实现用户登录并且输入错误三次后锁定该用户
2022-07-28 12:02:47
![](https://img.aspxhome.com/file/2023/0/123760_0s.jpg)
如何利用ASP实现邮箱访问
2007-09-29 12:27:00
详解用python生成随机数的几种方法
2022-01-24 14:17:42
Golang实现HTTP编程请求和响应
2024-04-28 09:10:42
![](https://img.aspxhome.com/file/2023/2/134312_0s.jpg)
python 实现查询Neo4j多节点的多层关系
2023-04-05 19:41:38
![](https://img.aspxhome.com/file/2023/0/99330_0s.jpg)
用python爬取分析淘宝商品信息详解技术篇
2022-09-12 23:40:54
![](https://img.aspxhome.com/file/2023/3/90933_0s.jpg)
一台linux主机启动多个MySQL数据库的方法
2024-01-14 00:46:00
![](https://img.aspxhome.com/file/2023/1/99501_0s.jpg)
Linux自动备份MySQL数据库脚本代码
2024-01-24 05:00:12
Django使用中间件解决前后端同源策略问题
2022-09-05 10:33:32
![](https://img.aspxhome.com/file/2023/3/71343_0s.png)
20年来我得到的20条编程经验
2010-05-26 13:44:00
Python实现给qq邮箱发送邮件的方法
2022-11-16 21:01:21
Python接口自动化浅析pymysql数据库操作流程
2024-01-16 16:33:02
![](https://img.aspxhome.com/file/2023/4/117964_0s.png)
Python探索之修改Python搜索路径
2021-06-26 20:47:09
Django + Uwsgi + Nginx 实现生产环境部署的方法
2023-01-01 06:02:49
![](https://img.aspxhome.com/file/2023/2/68982_0s.png)
Windows环境下python环境安装使用图文教程
2023-12-25 10:13:26
![](https://img.aspxhome.com/file/2023/9/123659_0s.png)