写一个图像到一个文本文件作为二进制数据C# [英] Writing an image to a text file as binary data C#

查看:109
本文介绍了写一个图像到一个文本文件作为二进制数据C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建嵌入图像作为一些记录内的文本文件。我有一些麻烦写图像作为文本。我在做什么是采集图像作为一个SQL数据库(图像类型)的字节数组,然后我通过每个字节会和写入该字节的ASCII相当于文件编写图像的文本文件。

I need to create a file that embeds an image as text within some records. I'm having some trouble writing the images as text. What I'm doing is gathering the image as a byte array from a SQL database (image type) then I'm writing that image to a text file by going through each byte and writing that byte's ASCII equivalent to the file.

在我可以写该图像到一个文本文件,我必须将它转换为TIFF(其前身是一家JPEG)的CCITT4格式。要仔细检查,这是正在做正确,我也流保存为TIFF和查看它在AsTiffTagViewer,这表明了压缩是正确的。我能够在适当的查看器来查看TIFF;然而,收集来自该文件的文本时,我无法查看图像。

Before I can write that image to a text file, I must convert it to a TIFF (it was formerly a jpeg) in CCITT4 format. To double check that this is being done correctly, I also save the stream as a TIFF and view it in "AsTiffTagViewer," which shows that the compression is correct. I AM able to view the tiff in a proper viewer; however, when gathering the text from the file, I am unable to view the image.

下面的代码:

byte[] frontImage = (byte[])imageReader["front_image"];
MemoryStream frontMS = new MemoryStream(frontImage);
Image front = Image.FromStream(frontMS);
Bitmap frontBitmap = new Bitmap(front);
Bitmap bwFront = ConvertToBitonal(frontBitmap);
bwFront.SetResolution(200, 200);
MemoryStream newFrontMS = new MemoryStream();
bwFront.Save(newFrontMS, ici, ep);
bwFront.Save("c:\\Users\\aarong\\Desktop\\C#DepositFiles\\" + checkReader["image_id"].ToString() + "f.tiff", ici, ep);
frontImage = newFrontMS.ToArray();   
String frontBinary = toASCII(frontImage); 

private String toASCII(byte[] image)
{
    String returnValue = "";
    foreach (byte imageByte in image)
    {
        returnValue += Convert.ToChar(imageByte);
    }
    return returnValue;
}   

有frontBinary说的被写入文件。有没有人有一个想法,什么是错的?中保存的的TIFF是正确的,但完全相同的字节数组,当为ASCII文本写的,没有被正确写入。

It is frontBinary that's being written to the file. Does anyone have an idea as to what is wrong? The tiff that's saved is correct, yet the exact same byte array, when written as ASCII text, is not being written correctly.

感谢您。

修改:该问题已使用的BinaryWriter(字节[])来正确地写图像作为文本修正。谢谢大家的帮助!

EDIT This issue has been corrected by using a BinaryWriter(byte[]) to correctly write the images as text. Thank you all for your help!

推荐答案

嘛ASCII只有七位,一件事。不过,我不相信您的代码实际上使用ASCII。它像是使用ISO-8859-1,含蓄。

Well ASCII is only seven-bit, for one thing. However, I don't believe your code actually uses ASCII. It sort of uses ISO-8859-1, implicitly.

从不的处理文本的二进制,反之亦然。它会的总是的导致问题

Never treat text as binary or vice versa. It will always lead to problems.

转换二进制到ASCII文本的最佳方法是使用Base64编码:

The best way of converting binary to ASCII text is to use Base64:

string text = Convert.ToBase64String(frontImage);
byte[] data = Convert.FromBaseString(text);



另外请注意,如果你的代码的没有的工作,它仍然是痛苦的低效 - 在 StringBuilders 念起来,然后再考虑你的代码是半相当于

Also note that if your code did work, it would still be painfully inefficient - read up on StringBuilders and then consider that your code is semi-equivalent to

Encoding.GetEncoding(28591).GetString(data);



不过,BASE64肯定是要去的文本和无损二进制数据之间进行转换的方式。你需要将其转换为二进制以再次查看课程的TIFF。

However, base64 is definitely the way to go to convert between text and binary data losslessly. You'll need to convert it back to binary in order to view the TIFF again, of course.

请注意,你有没有向你展示是如何保存或加载您的数据 - 你可能有问题,有太多。事实上,我怀疑如果你能够准确地保存字符串,您的可能的是幸运和保留数据,取决于你用它做什么......但走的base64反正。

Note that you haven't shown how you're saving or loading your data - you may have problems there too. In fact, I suspect that if you were able to save the string accurately, you might have been lucky and preserved the data, depending on exactly what you're doing with it... but go with base64 anyway.

这篇关于写一个图像到一个文本文件作为二进制数据C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆