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

首頁(yè) > 編程 > C# > 正文

C#實(shí)現(xiàn)把圖片轉(zhuǎn)換成二進(jìn)制以及把二進(jìn)制轉(zhuǎn)換成圖片的方法示例

2019-10-29 21:09:44
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了C#實(shí)現(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)實(shí)二進(jìn)制流代表的圖片public void ShowImgByByte(byte[] imgBytesIn){ MemoryStream ms = new MemoryStream(imgBytesIn); pictureBox1.Image = Image.FromStream(ms);}

二、將圖片保存到數(shù)據(jù)庫(kù)中,并從數(shù)據(jù)庫(kù)中讀取:

#region 將圖片從數(shù)據(jù)庫(kù)中讀取/// <summary>/// 將圖片從數(shù)據(jù)庫(kù)中讀取/// </summary>/// <param name="xs_ID">要讀取圖片的學(xué)號(hào)</param>/// <param name="ph">pictureBox1控件名</param>public void get_photo(string xs_ID, PictureBox ph)//將圖片從數(shù)據(jù)庫(kù)中讀取{ 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)//將圖片以二進(jìn)制存入數(shù)據(jù)庫(kù)中{ 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

希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到c#教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 上饶市| 海城市| 三明市| 航空| 溆浦县| 盈江县| 永济市| 枣强县| 建水县| 东明县| 泰来县| 东乡族自治县| 古蔺县| 张家口市| 班玛县| 成武县| 青浦区| 瑞昌市| 鲁山县| 涟水县| 成安县| 五寨县| 高雄市| 博乐市| 久治县| 宝丰县| 松原市| 洱源县| 临汾市| 曲阜市| 吴堡县| 荔浦县| 舒兰市| 泸州市| 南和县| 井陉县| 西青区| 五家渠市| 中卫市| 宣威市| 英山县|