然而,现在我面临的问题是尝试在.NET应用程序中显示这些图片,在尝试了无数天不同的操作字节数组的方式后,我已经快要放弃了。我尝试了至少8种不同的建议解决方案,每一种都以“参数无法识别”异常结束,当执行Image.fromStream()时。以下是到目前为止让我最接近的代码:
private void imageExtractTest()
{
LogOnDataSetTableAdapters.QueriesTableAdapter qa =
new LogOnDataSetTableAdapters.QueriesTableAdapter();
object docO = qa.GetLogonImage();
if (docO == null || !(docO is byte[]))
{
return;
}
byte[] doc = (byte[])docO;
MemoryStream ms = new MemoryStream();
ms.Write(doc, 0, doc.Length);
int firstByte;
int secondByte;
ms.Seek(0, SeekOrigin.Begin);
firstByte = ms.ReadByte();
secondByte = ms.ReadByte();
if (firstByte != 0x15 && secondByte != 0x1C)
{
//ErrorResponse("Stored object is not an Access File.");
return;
}
int fileTypeLoc = 20; // begin of the file type
short offset; // end of the file type
byte[] buffer = new byte[2];
ms.Read(buffer, 0, 2);
offset = BitConverter.ToInt16(buffer, 0);
long seekTotal = 0;
seekTotal += offset;
string docType = String.Empty;
for (int i = fileTypeLoc; i < offset; i++)
{
docType += (char)doc[i];
}
//if I query docType now I get 'Picture\0\0'
// magic eight bytes 01 05 00 00 03 00 00 00
ms.Seek(seekTotal, SeekOrigin.Begin);
buffer = new byte[8];
ms.Read(buffer, 0, 8);
seekTotal += 8;
// Second offset to move to
buffer = new byte[4];
ms.Read(buffer, 0, 4);
seekTotal += 4;
long offset2 = BitConverter.ToInt32(buffer, 0);
seekTotal += offset2;
ms.Seek(seekTotal, SeekOrigin.Begin);
// eight empty bytes
buffer = new byte[8];
ms.Read(buffer, 0, 8);
seekTotal += 8;
// next n bytes are the length of the file
buffer = new byte[4];
ms.Read(buffer, 0, 4);
seekTotal += 4;
long fileByteLength = BitConverter.ToInt32(buffer, 0);
// next N bytes are the file
byte[] data = new byte[fileByteLength];
// store file bytes in data buffer
ms.Read(data, 0, Convert.ToInt32(fileByteLength));
MemoryStream imageStream = new MemoryStream(data);
Image test = Image.FromStream(imageStream);
}
这段代码是从这里改编来的:这里,由于我只处理图像,所以不需要各种文档类型的识别,但是图像类型可以是任何东西——jpg、bmp、gif、png等等。
我也尝试保存输出的字节数组,但我没有成功查看它。但是当我将访问数据库并让它查看时,一切都很好。此外,.NET Crystal Report设计师能够以某种方式获取这些图像——因此它们必须在其中某个地方...
有人有什么想法吗?
马龙