Hi,
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!