中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

java中如何把圖片轉(zhuǎn)換成二進(jìn)制流

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
  1.將Image圖像文件存入到數(shù)據(jù)庫(kù)中 我們知道數(shù)據(jù)庫(kù)里的Image類型的數(shù)據(jù)是"二進(jìn)制數(shù)據(jù)",因此必須將圖像文件轉(zhuǎn)換成字節(jié)數(shù)組才能存入數(shù)據(jù)庫(kù)中。
   //根據(jù)文件名(完全路徑)
     
    public byte[] SetImageToByteArray(string fileName)
     
    { FileStream fs = new FileStream(fileName, FileMode.Open);
     
    int streamLength = (int)fs.Length; byte[] image = new byte[streamLength];
     
    fs.Read(image, 0, streamLength);
     
    fs.Close();
     
    return image; }
     
    //另外,在ASP.NET中通過FileUpload控件得到的圖像文件可以通過以下方法
     
    public byte[]
     
    SetImageToByteArray(FileUpload FileUpload1)
     
    { Stream stream = FileUpload1.PostedFile.InputStream;
     
    byte[] photo = new byte[FileUpload1.PostedFile.ContentLength];
     
    stream.Read(photo, 0, FileUpload1.PostedFile.ContentLength);
     
    stream.Close();
     
    return photo;
     
    }
     
    2.從SQL Server數(shù)據(jù)庫(kù)讀取Image類型的數(shù)據(jù),并轉(zhuǎn)換成bytes[]或Image圖像文件
    //要使用SqlDataReader要加載using System.Data.SqlClient命名空間
     
    //將數(shù)據(jù)庫(kù)中的Image類型轉(zhuǎn)換成byte[] public byte[] SetImage(SqlDataReader reader)
     
    { return (byte[])reader["Image"];//Image為數(shù)據(jù)庫(kù)中存放Image類型字段 }
     
    //將byte[]轉(zhuǎn)換成Image圖像類型 //加載以下命名空間using System.Drawing;/using System.IO;
     
    using System.Data.SqlClient;*/ public Image SetByteToImage(byte[] mybyte)
     
    { Image image; MemoryStream mymemorystream = new MemoryStream(mybyte,0, mybyte.Length);
     
    image = Image.FromStream(mymemorystream);
     
    return image;
     
    }


標(biāo)簽: 數(shù)據(jù)庫(kù)

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:字符串匹配的算法(暴力算法和KMP算法)

下一篇:mysql備份腳本