ASP.Net將圖片以二進位方式存入資料庫,並讀取

來源:互聯網
上載者:User

標籤:

把圖片轉換成二進位--把二進位轉換成圖片

        private void button1_Click(object sender, EventArgs e)        {            string path = this.textBox1.Text;            byte[] imgBytesIn = SaveImage(path);            ShowImgByByte(imgBytesIn);            //Parameters.Add("@Photo", SqlDbType.Binary).Value = imgBytesIn;                   }        //將圖片以二進位流        public byte[] SaveImage(String path)        {                        FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); //將圖片以檔案流的形式進行儲存            BinaryReader br = new BinaryReader(fs);            byte[] imgBytesIn = br.ReadBytes((int)fs.Length);  //將流讀入到位元組數組中            return imgBytesIn;        }        //現實二進位流代表的圖片        public void ShowImgByByte(byte[] imgBytesIn)        {            MemoryStream ms = new MemoryStream(imgBytesIn);            pictureBox1.Image = Image.FromStream(ms);        }

二、將圖片儲存到資料庫中,並從資料庫中讀取:

        #region 將圖片從資料庫中讀取        /// <summary>        /// 將圖片從資料庫中讀取        /// </summary>        /// <param name="xs_ID">要讀取圖片的學號</param>        /// <param name="ph">pictureBox1控制項名</param>        public void get_photo(string xs_ID, PictureBox ph)//將圖片從資料庫中讀取        {            byte[] imagebytes = null;            getcon();            SqlCommand con = new SqlCommand("select * from S_jiben where S_num=‘" + xs_ID + "‘", link);            SqlDataReader dr = con.ExecuteReader();            while (dr.Read())            {                imagebytes =(byte[])dr.GetValue(18);            }            dr.Close();            con_close();            MemoryStream ms = new MemoryStream(imagebytes);            Bitmap bmpt = new Bitmap(ms);            ph.Image = bmpt;         }        #endregion        #region        public void SaveImage(string MID, OpenFileDialog openF)//將圖片以二進位存入資料庫中        {            string strimg = openF.FileName.ToString();  //記錄圖片的所在路徑            FileStream fs = new FileStream(strimg, FileMode.Open, FileAccess.Read); //將圖片以檔案流的形式進行儲存            BinaryReader br = new BinaryReader(fs);            byte[] imgBytesIn = br.ReadBytes((int)fs.Length);  //將流讀入到位元組數組中            getcon();            StringBuilder strSql = new StringBuilder();            strSql.Append("update S_jiben Set [email protected] where S_num=" + MID);            SqlCommand cmd = new SqlCommand(strSql.ToString(), link);            cmd.Parameters.Add("@Photo", SqlDbType.Binary).Value = imgBytesIn;            cmd.ExecuteNonQuery();            con_close();        }        #endregion

結伴旅遊網www.jieberu.com
推推族,免費門票擷取平台 www.tuituizu.com

ASP.Net將圖片以二進位方式存入資料庫,並讀取

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.