我將圖像轉換爲字節數組並使用以下代碼存儲在文本文件中。我也正在成功檢索它們。C# - 圖像的高質量字節數組轉換
我擔心檢索圖像的質量不符合預期。有沒有更好的轉換爲字節數組和檢索的方法?我並不擔心太空概念。
請分享您的想法。
string plaintextStoringLocation = @"D:\ImageSource\Cha5.txt";
string bmpSourceLocation = @"D:\ImageSource\Cha50.bmp";
////Read image
Image sourceImg = Image.FromFile(bmpSourceLocation);
////Convert to Byte[]
byte[] clearByteArray = ImageToByteArray(sourceImg);
////Store it for future use (in plain text form)
StoreToLocation(clearByteArray, plaintextStoringLocation);
//Read from binary
byte[] retirevedImageBytes = ReadByteArrayFromFile(plaintextStoringLocation);
//Retrieve from Byte[]
Image destinationImg = ByteArrayToImage(retirevedImageBytes);
//Display Image
pictureBox1.Image = destinationImg;
編輯:解決的辦法是 - 使用Base64
//Plain Text Storing Location
string plaintextStoringLocation = @"D:\ImageSource\GirlInflower23.txt";
string bmpSourceLocation = @"D:\ImageSource\GirlInflower1.bmp";
////Read image
Image sourceImg = Image.FromFile(bmpSourceLocation);
string base64StringOfIMage = ImageToBase64(sourceImg, ImageFormat.Bmp);
byte[] byteOfString = Convert.FromBase64String(base64StringOfIMage);
StoreToLocation(byteOfString, plaintextStoringLocation);
byte[] retrievedBytesForStrimngForImage = ReadByteArrayFromFile(plaintextStoringLocation);
MemoryStream memStream = new MemoryStream(retrievedBytesForStrimngForImage);
//memStream.Read();
Image retrievedImg = Image.FromStream(memStream);
pictureBox1.Image = retrievedImg;
什麼是ImageToByteArray,ReadByteArrayFromFile和ByteArrayToImage?這是你自己編寫的代碼嗎? – 2010-06-17 20:05:06
轉換爲'byte []'時,是否使用相同的編碼,寫入和讀取文件? – Oded 2010-06-17 20:07:15