如何循环的------------------把数据库里的Image字段,导出成图片文件

雄牛 2018-03-13 07:41:02

是这样,原来的设计是把图片存在数据库表Report中的,字段img的类型为image,但后来数据库越来越大,所以
要做一个工具去升级,把原存的img字段循环导出成实体文件,文件都是jpg后缀.

这个代码逻辑应该怎么写?

下面是部份代码

SqlConnection conn = new SqlConnection(DbHelper.connectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("select ReportNo,Images,Images from Report Order by Rid", conn);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();

MemoryStream buf = new MemoryStream((byte[])reader[0]);
Image image = Image.FromStream(buf, true);
// pictureBox1.Image = image;
...全文
558 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
kunshan 2018-03-14
  • 打赏
  • 举报
回复
public int FileToStream()//文件到流的转换 { Image img = new Bitmap(SourFilePath); MemoryStream stream = new MemoryStream(); img.Save(stream, ImageFormat.Bmp); BinaryReader br = new BinaryReader(stream); photo = stream.ToArray(); stream.Close(); return 0; } public Image ShowPic()//根据流显图 { byte[] bytes = photo; MemoryStream ms = new MemoryStream(bytes); ms.Position = 0; Image img = Image.FromStream(ms); ms.Close(); return img; }
就是个菜鸟 2018-03-14
  • 打赏
  • 举报
回复
你将数据读出成流,又将流转为图片,接下来不会了么?
读肯定要循环的,你这样就读了一个。

while (reader.Read())
{
data = (byte[])reader[0];
//根据字节数组创建内存流
ms = new MemoryStream(data);
//根据内存流创建图片
Image image = Image.FromStream(ms);
//将图片保存
//定义图片需要存放的文件夹位置
string path = "";
//判断该文件夹是否存在
if (!Directory.Exists(path))
{
//如果不存在创建文件夹
var dic = Directory.CreateDirectory(path);
}
//保存图片为JPG格式,图片名用:全局唯一标识
image.Save(path + "\\" + Guid.NewGuid() + ".jpg");
二楞子1983 2018-03-14
  • 打赏
  • 举报
回复
最好是先加一个标记栏位默认为0,每次取标记为0的top1000,然后循环保存图片文件,更新数据并标记为1. 依次循环
  • 打赏
  • 举报
回复
新建一个文件夹,保存时将图片保存在文件夹里,数据库只保存文件路径,读的时候读路径就可以了

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧