Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing an image to a text file as binary data C#

Tags:

c#

stream

image

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.

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.

Here's the code:

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;
}   

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.

Thank you.

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

like image 632
Aaron Avatar asked May 19 '09 21:05

Aaron


1 Answers

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.

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

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

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);

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.

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.

like image 148
Jon Skeet Avatar answered Nov 15 '22 15:11

Jon Skeet