zoukankan      html  css  js  c++  java
  • 从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();
    
    
            }
        }
    }
    

      

  • 相关阅读:
    【POJ】3243 Clever Y
    【BZOJ】3916: [Baltic2014]friends
    【URAL】1960. Palindromes and Super Abilities
    【BZOJ】3621: 我想那还真是令人高兴啊
    【BZOJ】2286: [Sdoi2011消耗战
    【POJ】2891 Strange Way to Express Integers
    鬼畜的多项式
    【CF】438E. The Child and Binary Tree
    【BZOJ】3456: 城市规划
    【POJ】1811 Prime Test
  • 原文地址:https://www.cnblogs.com/ChineseMoonGod/p/4337804.html
Copyright © 2011-2022 走看看