/* 將圖片轉換為64位編碼 */
//找到文件夾 System.IO.DirectoryInfo dd = new System.IO.DirectoryInfo("C://qq"); //得到該文件夾下所有的文件 System.IO.FileInfo[] file = dd.GetFiles(); //得到某個文件的路徑 string filePath = file[1].DirectoryName + "\\" + file[1].Name; //用streamread讀這個文件 System.IO.StreamReader sr = new StreamReader(filePath, Encoding.Default, true); int index; //實例化一個內存流 System.IO.MemoryStream tempStream = new MemoryStream(); //將流轉換為字節數組 while ((index = sr.BaseStream.ReadByte()) != -1) { tempStream.WriteByte(((byte)index)); } byte[] array = tempStream.ToArray(); tempStream.Close(); //將得到的字節數組轉換為base64位編碼 string result = Convert.ToBase64String(array); System.IO.File.WriteAllText("C://zz.txt", result);
/* 將64位編碼轉換為圖片 */
//將64位編碼轉換為字節數組 byte[] cc = Convert.FromBase64String(result); //用filestream創造一個文件 FileStream fs = new FileStream("C://a.jpg", FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); bw.Write(cc);