【发布时间】:2010-10-27 11:31:57
【问题描述】:
我需要创建一个文件,将图像作为文本嵌入到某些记录中。我在将图像写为文本时遇到了一些麻烦。我正在做的是将图像作为字节数组从 SQL 数据库(图像类型)收集,然后我通过遍历每个字节并将该字节的 ASCII 等效于文件写入该图像到文本文件。
在将该图像写入文本文件之前,我必须将其转换为 CCITT4 格式的 TIFF(以前是 jpeg)。为了仔细检查这是否正确完成,我还将流保存为 TIFF 并在“AsTiffTagViewer”中查看它,这表明压缩是正确的。我能够在适当的查看器中查看 tiff;但是,从文件中收集文本时,我无法查看图像。
代码如下:
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 文本时,写入不正确。
谢谢。
编辑 已通过使用 BinaryWriter(byte[]) 将图像正确地写入文本来纠正此问题。谢谢大家的帮助!
【问题讨论】: