日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

C#把圖片轉(zhuǎn)換字節(jié)存入數(shù)據(jù)庫(kù)在讀取顯示出來(lái)。

系統(tǒng) 2597 0
//獲取圖片的詳細(xì)信息 并把圖片轉(zhuǎn)換到字節(jié)?

private void ?button1_Click( object ?sender,?EventArgs?e)
????????{
???????????? string ?pPath?=?Application.StartupPath?+? @" \QQ.jpg " ;
????????????FileInfo?fi?=? new ?FileInfo(pPath);
???????????? byte []?Temp?=?imageToByteArray(pPath);
????????????Image?img?=?byteArrayToImage(Temp);
???????????? int ?W?=?img.Width;
???????????? int ?H?=?img.Height;

???????????? string ?Fm?=?fi.Extension; // 擴(kuò)展名
???????????? long ?size?=?fi.Length;? //
???????????? string ?Fn?=?fi.Name;
???????????? string ?info?=? " 名稱(chēng): " ?+?Fn?+? " ??分辨率: " ?+?W?+? " * " ?+?H;
????????????info?+=? " ??格式: " ?+?Fm?+? " ??大小: " ?+?((size?>? 1024 )???(( float )(( float )size?/? 1024.0 )).ToString( " 0.00 " )?+? " KB " ?:?size?+? " B " );
???????????? this .label1.Text?=?info;
???????????? this .pictureBox1.Height?=?H;
???????????? this .pictureBox1.Width?=?W;
???????????? this .pictureBox1.Image?=?img;
????????}

???????? private ? void ?button2_Click( object ?sender,?EventArgs?e)
????????{
???????????? string ?pPath?=?Application.StartupPath?+? @" \QQ.jpg " ;
???????????? byte []?Temp?=?imageToByteArray(pPath);
????????????StringBuilder?Sb?=? new ?StringBuilder();
???????????? for ?( int ?i?=? 0 ;?i?<?Temp.Length;?i++)
????????????{
????????????????Sb.Append(Temp[i].ToString());
????????????}
????????????richTextBox1.Text?=?Sb.ToString();

????????}
???????? /// ? <summary>
????????
/// ?圖片轉(zhuǎn)為Byte字節(jié)數(shù)組
????????
/// ? </summary>
????????
/// ? <param?name="FilePath"> 路徑 </param>
????????
/// ? <returns> 字節(jié)數(shù)組 </returns>
???????? private ? byte []?imageToByteArray( string ?FilePath)
????????{
???????????? using ?(MemoryStream?ms?=? new ?MemoryStream())
????????????{

???????????????? using ?(Image?imageIn?=?Image.FromFile(FilePath))
????????????????{

???????????????????? using ?(Bitmap?bmp?=? new ?Bitmap(imageIn))
????????????????????{
????????????????????????bmp.Save(ms,?imageIn.RawFormat);
????????????????????}

????????????????}
???????????????? return ?ms.ToArray();
????????????}
????????}
???????? /// ? <summary>
????????
/// ?字節(jié)數(shù)組生成圖片
????????
/// ? </summary>
????????
/// ? <param?name="Bytes"> 字節(jié)數(shù)組 </param>
????????
/// ? <returns> 圖片 </returns>
???????? private ?Image?byteArrayToImage( byte []?Bytes)
????????{
???????????? using ?(MemoryStream?ms?=? new ?MemoryStream(Bytes))
????????????{
????????????????Image?outputImg?=?Image.FromStream(ms);
???????????????? return ?outputImg;
????????????}
? ? ? ? ?}

?

?

//從數(shù)據(jù)庫(kù)里面讀取出來(lái)

private void button1_Click(object sender, EventArgs e)

??????? {

????????? openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";

??????????? if(openFileDialog1.ShowDialog()==DialogResult.OK)

??????????? {

????????????? string fullpath =openFileDialog1.FileName;//文件路徑

????????????? FileStream fs = new FileStream(fullpath, FileMode.Open);

??????????????? byte[] imagebytes =new byte[fs.Length];

??????????????? BinaryReader br = new BinaryReader(fs);

??????????????? imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));

??????????????? //打開(kāi)數(shù)據(jù)庫(kù)

??????????????? SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=;database=db_05");

??????????????? con.Open();

??????????????? SqlCommand com = new SqlCommand("insert into tb_08 values(@ImageList)",con);

??????????????? com.Parameters.Add("ImageList", SqlDbType.Image);

??????????????? com.Parameters["ImageList"].Value = imagebytes;

?????????????? com.ExecuteNonQuery();

?????????????? con.Close();

???????????? }????

}

?

C#把圖片轉(zhuǎn)換字節(jié)存入數(shù)據(jù)庫(kù)在讀取顯示出來(lái)。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 南澳县| 桂东县| 县级市| 梨树县| 延寿县| 阿拉尔市| 桑植县| 斗六市| 阿克苏市| 南澳县| 来宾市| 宿迁市| 石台县| 绥棱县| 隆回县| 肇州县| 东乡族自治县| 中牟县| 大埔县| 阿拉善左旗| 东城区| 吉水县| 永靖县| 海门市| 竹山县| 喀什市| 夏邑县| 阿克陶县| 龙岩市| 益阳市| 延津县| 泰兴市| 桃源县| 河南省| 无锡市| 水城县| 凯里市| 运城市| 贵州省| 胶州市| 巩留县|