讀取 資料庫 Image 型態欄位

讀取 資料庫 Image 型態欄位

            string conn = "連接字串";
            using (SqlConnection sqlcon = new SqlConnection(conn))
            {
                SqlCommand sqlcmd = new SqlCommand();
                sqlcmd.CommandText = " 讀取圖檔的SQLString";
                sqlcmd.Connection = sqlcon;
                sqlcon.Open();
                obj= sqlcmd.ExecuteScalar();
                sqlcon.Close();
            }

            
            Byte[] buffer = (Byte[])obj;
            FileStream fs = new FileStream("C:\\MyPic.jpg", FileMode.Create); //產生檔案串流
            fs.Write(buffer, 0, buffer.Length); //寫入
            fs.Close();

            MemoryStream ms = new MemoryStream(buffer);  //轉成記憶體串流
            this.pictureBox1.Image = Image.FromStream(ms); //載入圖片
            ms.Close(); //關閉串流