从MS Access转换OLE图像对象以在.NET中使用

Mar*_*lon 7 .net c# ms-access ole

我正在努力将基于Access的系统重新开发到c#.net,但是当MS从办公室2003到办公室2007时,他们删除了访问中的图片编辑器 - 这意味着以前存储的图片将不再显示在系统中.该公司的人做了一个黑客,基本上使用excel在后台保存了VBA图像(如果你需要它我可以获得更多信息)但基本上它意味着仍然可以使用访问图像控件(对象绑定帧).

但是,我现在遇到了尝试在.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);
    }
Run Code Online (Sandbox Code Playgroud)

这段代码是从这里改编的,我不需要各种doctypes识别,因为我只处理图像,但图像类型可以是任意数量的东西 - jpg,bmp,gif,png等.

我也尝试保存输出的字节数组,但我也没有运气查看.但是当我指向访问数据库并让它查看它时,一切都很好.此外,.NET Crystal Report设计器能够获得这些图像的方式 - 所以它们必须在某处...

有没有人有任何想法?

马龙

Ste*_*bob 3

尝试从 .NET 检索 MS-access OLE 图像字段令人头疼而不值得。这篇文章中有一些关于这个主题的很好的讨论和信息。

最终,成功实现此目的的最佳且最简单的解决方案是使用工作查看方法将这些图像保存为单独的文件,然后将这些文件作为 BLOB 字段而不是图像字段导入到数据库中。然后您可以轻松地将它们读入.NET。