程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 圖片轉二進制流存儲到數據庫,二進制數據庫

圖片轉二進制流存儲到數據庫,二進制數據庫

編輯:C#入門知識

圖片轉二進制流存儲到數據庫,二進制數據庫


1.數據庫名為Demo,數據結構如圖

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;
        }
    }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved