从sql中image类型字段中导出图片
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Data.SqlClient; using System.Data; using System.Drawing.Imaging; using System.Drawing; namespace 获取名字 { class Program { static void Main(string[] args) { string path = @"C:\Users\Chinaese\Desktop\_FabricImg"; string x = Path.GetFileName(Path.GetDirectoryName(path)); DirectoryInfo root = new DirectoryInfo(path); // DirectoryInfo[] childDirs = root.GetDirectories(); FileInfo[] files = root.GetFiles(); List<string> filename = new List<string>(); foreach (FileInfo item in files) { filename.Add(Path.GetFileNameWithoutExtension(item.FullName)); } string connectionString=@"Data Source = 192.168.0.180; Initial Catalog = pms; User Id = sa; Password = hualong2012;"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand("SELECT FlowerCode,PicImg FROM FlowerPics where flowerid IN ( SELECT MAX(flowerid) AS Expr1 FROM FlowerPics GROUP BY FlowerCode )", connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); // 判断数据是否读到尾. while (reader.Read()) { //声明数组,用于保存数据库的二进制数据 byte[] mybyte = null; //读取数据保存到数组中 try { if (filename.Contains(reader[0])) { continue; } mybyte = (byte[])reader["PicImg"]; Image image; //读取数组数据成为文件流 MemoryStream mymemorystream = new MemoryStream(mybyte); //转换成为图片格式。 image = Image.FromStream(mymemorystream, true); //image.Save(mymemorystream, ImageFormat.Jpeg); image.Save(@"C:\Users\Chinaese\Desktop\SAMP\" + reader[0].ToString() + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp); mymemorystream.Close(); //关闭流 filename.Add(reader[0].ToString()); } catch (Exception) { //break; } finally { } //Console.WriteLine(String.Format("{0}, {1}", reader[0], reader[1])); } // 一定要关闭 reader 对象. reader.Close(); } Console.ReadKey(); } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。