国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現把圖片轉換成二進制以及把二進制轉換成圖片的方法示例

2020-01-24 00:38:06
字體:
來源:轉載
供稿:網友

本文實例講述了C#實現把圖片轉換成二進制以及把二進制轉換成圖片的方法。分享給大家供大家參考,具體如下:

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

二、將圖片保存到數據庫中,并從數據庫中讀?。?/p>

#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#regionpublic 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 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#相關內容感興趣的讀者可查看本站專題:《C#圖片操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結》、《C#數據結構與算法教程》、《C#面向對象程序設計入門教程》及《C#程序設計之線程使用技巧總結

希望本文所述對大家C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中方县| 怀仁县| 定边县| 库伦旗| 宝兴县| 屯留县| 漳浦县| 右玉县| 井研县| 武城县| 若羌县| 汶上县| 广西| 昌邑市| 贵定县| 如东县| 成都市| 林西县| 朝阳市| 平度市| 炎陵县| 叙永县| 南陵县| 庄河市| 商南县| 新源县| 安化县| 古丈县| 无为县| 邯郸市| 汕头市| 霞浦县| 杭锦旗| 海安县| 灌云县| 崇明县| 丰台区| 和政县| 开江县| 梁山县| 文山县|