【发布时间】:2010-06-17 20:01:00
【问题描述】:
我正在使用以下代码将图像转换为字节数组并存储在文本文件中。我也在成功检索它们。
我担心检索到的图像质量达不到预期。有没有办法更好地转换为字节数组和检索?我不担心空间概念。
请分享你的想法。
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?这段代码是你自己写的吗?
-
在转换为
byte[]、写入和读取文件时是否使用相同的编码?
标签: c#