从MS Access用于.NET转换的OLE图像对象图像、对象、Access、MS

2023-09-03 01:28:57 作者:┈┾侞淉︶ㄣ

我正在重新开发一个访问的系统为C#.NET,但是当从MS Office 2003的到办公室2007年,他们取出内进入图片编辑器 - 这意味着previously存储的照片会在不再显示系统。该人在该公司做了一个黑客,基本上保存图像使用Excel在后台VBA(如果你需要它,我可以得到更多的信息),但基本上它意味着获得图像控制仍可使用(对象约束帧)。

I'm working on redeveloping an Access based system into c#.net, however when MS went from office 2003 to office 2007 they removed the picture editor within access - which meant that previously stored pictures would no longer display in the system. The guys at the company did a hack that basically saved the images with VBA using excel in the background (I can get more information if you need it) but basically it meant the access image controls could still be used (Object bound frames).

不过,我现在试图在.NET应用程序中显示这些问题,之后试图操纵字节数组不同的方式无数个我靠近放弃。我曾尝试至少有8个不同的建议的解决方案,每一个具有'参数无法识别异常做I​​mage.fromStream结束时,()。以下是已经让我最接近到目前为止code:

However, I now have the problem of trying to display these in .NET applications, and after countless days of trying different ways of manipulating the byte array I'm close to giving up. I have tried at least 8 different suggested solutions and each one ends with a 'Parameter not recognised' exception when doing Image.fromStream(). Below is the code which has got me the closest so far:

    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);
    }

这code改编自这里,我并不需要的各种文档类型识别因为我只处理图像,但图像类型可以是任意数量的东西 - JPG,BMP,GIF,PNG等。

This code was adapted from here, I didn't need the various doctypes identification as I'm only dealing with images, however the image type could be any number of things - jpg, bmp, gif, png etc.

我也试着保存输出字节数组,但我没有运气或者观看的。但是,当我点对数据库的访问,并得到它来查看它,一切都很好。另外,.NET水晶报表设计器能够获得这些图像一些如何 - 所以他们必须在那里的某个地方......

I've also tried saving the outputted byte array but I've had no luck viewing that either. But when I point access to the database and get it to view it, everything is fine. Also the .NET Crystal Report designer is able to get these images some how - so they must be in there somewhere...

有没有人有什么想法?

马龙

推荐答案

尝试从.NET检索MS访问OLE image字段的方式比较头痛的比它的价值。有一些很好的讨论和信息关于此主题的this帖子。

Trying to retrieve an MS-access OLE image field from .NET is way more headache than it is worth. There is some good discussion and info about this topic in this post.

最后,你最好的和最简单的,解决办法是成功的,这是使用你的工作的观看方法来保存这些影像为单独的文件,然后导入这些文件到数据库中的BLOB字段,而不是像场。然后,你可以很容易地阅读到.NET。

Ultimately, your best, and easiest, solution to be successful with this is to use your working viewing method to save these images as separate files, then import those files into the database as BLOB fields, rather than image fields. Then you can easily read them into .NET.