您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)C#如何實現(xiàn)把圖片轉(zhuǎn)換成二進(jìn)制以及把二進(jìn)制轉(zhuǎn)換成圖片,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
具體如下:
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; } //將圖片以二進(jìn)制流 public byte[] SaveImage(String path) { FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); //將圖片以文件流的形式進(jìn)行保存 BinaryReader br = new BinaryReader(fs); byte[] imgBytesIn = br.ReadBytes((int)fs.Length); //將流讀入到字節(jié)數(shù)組中 return imgBytesIn; } //現(xiàn)實二進(jìn)制流代表的圖片 public void ShowImgByByte(byte[] imgBytesIn) { MemoryStream ms = new MemoryStream(imgBytesIn); pictureBox1.Image = Image.FromStream(ms); }
二、將圖片保存到數(shù)據(jù)庫中,并從數(shù)據(jù)庫中讀?。?/p>
#region 將圖片從數(shù)據(jù)庫中讀取 /// <summary> /// 將圖片從數(shù)據(jù)庫中讀取 /// </summary> /// <param name="xs_ID">要讀取圖片的學(xué)號</param> /// <param name="ph">pictureBox1控件名</param> public void get_photo(string xs_ID, PictureBox ph)//將圖片從數(shù)據(jù)庫中讀取 { 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)//將圖片以二進(jìn)制存入數(shù)據(jù)庫中 { string strimg = openF.FileName.ToString(); //記錄圖片的所在路徑 FileStream fs = new FileStream(strimg, FileMode.Open, FileAccess.Read); //將圖片以文件流的形式進(jìn)行保存 BinaryReader br = new BinaryReader(fs); byte[] imgBytesIn = br.ReadBytes((int)fs.Length); //將流讀入到字節(jié)數(shù)組中 getcon(); StringBuilder strSql = new StringBuilder(); strSql.Append("update S_jiben Set xs_photo=@Photo where S_num=" + MID); SqlCommand cmd = new SqlCommand(strSql.ToString(), link); cmd.Parameters.Add("@Photo", SqlDbType.Binary).Value = imgBytesIn; cmd.ExecuteNonQuery(); con_close(); } #endregion
C#是一個簡單、通用、面向?qū)ο蟮木幊陶Z言,它由微軟Microsoft開發(fā),繼承了C和C++強(qiáng)大功能,并且去掉了一些它們的復(fù)雜特性,C#綜合了VB簡單的可視化操作和C++的高運行效率,以其強(qiáng)大的操作能力、優(yōu)雅的語法風(fēng)格、創(chuàng)新的語言特性和便捷的面向組件編程從而成為.NET開發(fā)的首選語言,但它不適用于編寫時間急迫或性能非常高的代碼,因為C#缺乏性能極高的應(yīng)用程序所需要的關(guān)鍵功能。
關(guān)于“C#如何實現(xiàn)把圖片轉(zhuǎn)換成二進(jìn)制以及把二進(jìn)制轉(zhuǎn)換成圖片”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。