图片转二进制流存储到数据库

时间:2021-07-04 21:45:09

小子菜鸟一枚,刚毕业,第一次写博客,经历了一段工作经历后觉得还是觉得得把一些东西记录下来才好,最近在搞数据库方面的知识,以后会贴一些数据库方面的的帖子 ,用来加深记忆的!

1.数据库名为Demo,数据结构如图,image类型是用来保存byte[]数组的

图片转二进制流存储到数据库

2.后台代码如下

 class Program
    {
        public static readonly string conStr = "Data Source = .;Initial Catalog = Demo;Integrated Security = SSPI;";
        static void Main(string[] args)
        {
            #region 图片转为二进制流写进数据库
            byte[] buffer = ImageToByte(@"G:\2.jpg");
            if (PushDataBase(buffer) > 0)
            {
                Console.WriteLine("OK");
                Console.Read();
            }
            #endregion

            #region 二进制流转图片
            //byte[] gBuffer = GetDataBase(3);
            //ByteToImage(gBuffer);
            //Console.WriteLine("OK");
            //Console.Read();
            #endregion

        }

        /// <summary>
        /// 图片转二进制流
        /// </summary>
        /// <param name="imgPath">图片路径</param>
        /// <returns></returns>
        public static byte[] ImageToByte(string imgPath)
        {
            Image image = Image.FromFile(imgPath);
          
            using (MemoryStream ms = new MemoryStream())
            {
                image.Save(ms, image.RawFormat);
                byte[] buffer = new byte[ms.Length];
                ms.Seek(0, SeekOrigin.Begin);
                ms.Read(buffer, 0, buffer.Length);
                return buffer;
            }
        }

        /// <summary>
        /// 字节流转图片
        /// </summary>
        /// <param name="buffer">图片二进制流</param>
        public static void ByteToImage(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream();

            ms.Write(buffer, 0, buffer.Length);
            Image img = Image.FromStream(ms);
            string file = "mypicture2";
            if (img.RawFormat == ImageFormat.Jpeg)
            {
                file += ".jpg";

            }
            else if (img.RawFormat == ImageFormat.Png)
            {
                file += ".png";
            }
            else
            {
                file += ".jpg";
            }
            File.WriteAllBytes(file, buffer);
        }
        /// <summary>
        /// 写进数据库
        /// </summary>
        /// <param name="buffer">图片二进制流</param>
        /// <returns></returns>
        public static int PushDataBase(byte[] buffer)
        {
            using (SqlConnection conn = new SqlConnection(conStr))
            {
                using (SqlCommand com = new SqlCommand())
                {
                    com.Connection = conn;
                    conn.Open();
                    com.CommandText = "insert into ImageData values(@image)";
                    com.Parameters.Add("@image", SqlDbType.Image).Value = buffer;
                    return com.ExecuteNonQuery();
                }
            }
        }
        /// <summary>
        /// 从数据库中读取
        /// </summary>
        /// <returns></returns>
        public static byte[] GetDataBase(int id)
        {
            using (SqlConnection conn = new SqlConnection(conStr))
            {
                using (SqlCommand com = new SqlCommand())
                {
                    com.Connection = conn;
                    conn.Open();
                    com.CommandText = "select ImageByte from ImageData where Id=@id";
                    com.Parameters.Add("@id", SqlDbType.Int).Value = id;
                    using (SqlDataReader reader = com.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            return (byte[])reader[0];
                        }
                    }

                }
            }
            return null;
        }
    }

 好了,就到这了,不够150字居然不给发布了,我凑够字数先。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。