【发布时间】:2011-01-25 20:49:03
【问题描述】:
我正在努力将基于 Access 的系统重新开发为 c#.net,但是当 MS 从 office 2003 转到 office 2007 时,他们删除了 access 中的图片编辑器 - 这意味着以前存储的图片将不再显示在系统中。该公司的人做了一个 hack,基本上使用 VBA 在后台使用 excel 保存图像(如果需要,我可以获得更多信息)但基本上这意味着仍然可以使用访问图像控件(对象绑定帧)。
但是,我现在遇到了在 .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);
}
此代码改编自here,我不需要各种文档类型识别,因为我只处理图像,但是图像类型可以是任意数量的东西 - jpg、bmp、gif、png 等。
我也尝试过保存输出的字节数组,但我也没有运气查看它。但是当我指向数据库并让它查看它时,一切都很好。 .NET Crystal Report 设计器也能够以某种方式获取这些图像 - 所以它们必须在某个地方......
有人有什么想法吗?
马龙
【问题讨论】: