1. 程式人生 > 其它 >圖片位元組流和字串互轉,讀存圖片。

圖片位元組流和字串互轉,讀存圖片。

        public void ReadandWritePicture()
        {
            //開始使用流讀取  
            FileStream fs = new FileStream("D:\\A.jpg", FileMode.Open, FileAccess.Read);
            //使用流讀取器,把檔案流物件中的內容讀取出來,轉換成字串或者其他對應的資料
            BinaryReader br = new BinaryReader(fs);//二進位制讀取器
            byte[] imgbytes = br.ReadBytes((int)fs.Length);//將流中資料讀取成byte陣列存入陣列變數中
            string textString = System.Convert.ToBase64String(imgbytes);//轉為字串

            //字串轉為byte陣列
            byte[] imgb= System.Convert.FromBase64String(textString);
            //存進指定目錄
            System.IO.File.WriteAllBytes("D:\\B.jpg", imgb);

        }
            

1、讀指定目錄的圖片

2、轉為byte[]

3、byte[]轉為string

4、string轉為byte[]

5、byte[]存在其他目錄為圖片

除非不再醒來,除非太陽不再升起,不然都請你好好生活,掙扎著前進,開心的笑。