Csharp/C#教程:C# – 图像的高质量字节数组转换分享


C# – 图像的高质量字节数组转换

我正在使用以下代码将图像转换为字节数组并存储在文本文件中。 我也成功地检索了它们。

我担心的是检索图像的质量达不到预期。 有没有办法更好地转换为字节数组并检索? 我并不担心空间概念。

请分享你的想法。

string plaintextStoringLocation = @"D:ImageSourceCha5.txt"; string bmpSourceLocation = @"D:ImageSourceCha50.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:ImageSourceGirlInflower23.txt"; string bmpSourceLocation = @"D:ImageSourceGirlInflower1.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; 

是的,可以获得完全无损的存储。 如果您只是以原始的BMP格式存储它将没有问题。 我假设您正在将其转换为文本,因为您希望通过某些协议发送它,其中二进制字符将被破坏。

您可以考虑使用Convert.ToBase64String而不是您正在做的任何事情。

我对这个片段没有任何问题…尝试一下……如果你得到了好的结果那么问题出在你的Image – > byte []或byte [] – >图像代码:)

上述就是C#学习教程:C# – 图像的高质量字节数组转换分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 Image srcImage; Image destImage; // load an image srcImage = Image.FromFile(filename); // save the image via stream -> byte[] using(MemoryStream stream = new MemoryStream()){ image.Save(stream, ImageFormat.xxx); byte[] saveArray = stream.ToArray(); /*..... strore saveArray......*/ } // rehydrate byte[] loadArray = /*...get byte array from storage...*/ using(MemoryStream stream = new MemeoryStream(loadArray)){ destImage = Image.FromStream(stream); } pictureBox.Image = dstImage; // don't forget...dispose of any Image/Stream objects 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/954037.html

(0)
上一篇 2021年11月20日
下一篇 2021年11月20日

精彩推荐