• 从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:UsersChinaeseDesktop\_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:UsersChinaeseDesktopSAMP" + 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();
    
    
            }
        }
    }
    

      

  • 相关阅读:
    (大数 小数点) 大明A+B hdu1753
    (大数 万进制) N! hdu1042
    (next_permutation) 排列2 hdu 1716
    (set)产生冠军 hdu2094
    (Set) {A} + {B} hdu1412
    (set stringstream)单词数 hdu2072
    (set)MG loves gold hdu6019
    (set) 人见人爱A-B hdu2034
    (map)水果 hdu1263
    (map)What Are You Talking About hdu1075
  • 原文地址:https://www.cnblogs.com/ChineseMoonGod/p/4337804.html
Copyright © 2020-2023  润新知