我将图像转换为字节数组并使用以下代码存储在文本文件中。我也正在成功检索它们。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