从 Access 读取图像 - 参数无效 [英] Reading image from Access - parameter not valid

查看:23
本文介绍了从 Access 读取图像 - 参数无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Access .mdb 文件中有一个简单的数据库,但我不知道如何处理:当我从流创建 Image 时,参数无效"异常.我读过我需要去除 78 个字节的偏移量(从这里)但我仍然收到参数无效"错误当我调用 FromStream 时,即使在剥离了前 78 个字节之后也是如此.

I have simple database in Access .mdb file, but I don't know how to deal with: "parameter not valid" exception when Im creating Image from stream. I'v read that I need to strip 78 bytes offset (from here) but I still get a "parameter not valid" error when I call FromStream, even after stripping off the first 78 bytes.

这对我不起作用:

byte[] abytPic = (byte[])dt.Rows[0]["Photo"]; byte arrary with image
if ((abytPic[0] == 21) && (abytPic[1] == 28)) //It's true
{
    byte[] abytStripped = new byte[abytPic.Length - 78];
    System.Buffer.BlockCopy(abytPic, 78, abytStripped, 0, abytPic.Length - 78); 
    msPic = new emoryStream(abytStripped);
}

推荐答案

如果您直接从 MS Access 读取数据,则不需要去除任何标题信息.

If you are reading the data directly from MS Access, you do not need to strip any header information.

假设图像存储为 BLOB,其中是最常见的,这里是从数据库中读取字节数组并将其存储为图像文件的代码(对不起,VB 而不是 C#):

Assuming the image is stored as a BLOB, which is the most common, here is code to read in the array of bytes from the database and store as an image file (sorry, VB instead of C#):

  Dim varBytes() As Byte

  Using cn As New OleDbConnection(myConnectionString)
     cn.Open()

     sqlText = "SELECT [myColumn] " _
              & "FROM [myTable] " _
              & "WHERE ([mySearchCriteria] = '" & mySearchTerm & "')"

     Using cm As New OleDbCommand(sqlText, cn)
        Dim rdr As OleDbDataReader

        rdr = cm.ExecuteReader

        rdr.Read()

        varBytes = rdr.GetValue(0)
     End Using

  End Using

  My.Computer.FileSystem.WriteAllBytes(myPath & "myFile.emf", varBytes, True)

我使用的这个示例是我知道数据库中的文件是 .emf 图像的示例.如果你知道扩展名,你可以把它放在文件名上.如果不这样做,您可以将其留空,然后使用图像查看器打开结果;它应该开始.如果您需要查找扩展名或文件类型,一旦将其保存为文件,您可以使用任何十六进制编辑器打开它,文件类型将从标题信息中找到.

This example that I had laying around is one where I knew the files in the database were .emf images. If you know the extension, you can put it on the file name. If you don't, you can leave it blank and then open the resulting with an image viewer; it should start. If you need to find the extension or file type, once it is saved as a file, you can open it with any hex editor and the file type will be available from the header information.

你的问题有点不清楚,所以我不确定上面的代码是否正是你想要的,但它应该让你更接近.

Your question is a little bit unclear, so I'm not sure the above code is exactly what you want, but it should get you a lot closer.

这是 VB 代码,它采用字节数组,将其加载到 MemoryStream 对象中,然后从 Stream 创建一个 Image 对象.这段代码工作得很好,并在我的表单上的图片框中显示了图像.

This is the VB code that takes the array of bytes, loads it into a MemoryStream object, and then creates an Image object from the Stream. This bit of code worked just fine, and displayed the image in a picturebox on my form.

  Dim img As Image
  Dim str As New MemoryStream(varBytes)
  img = Image.FromStream(str)
  PictureBox1.Image = img

如果等效的 C# 对您不起作用,则问题可能在于图像在 MS Access 数据库中的存储方式.

If the C# equivalent of this is not working for you, then the problem is likely in how the image is stored in the MS Access database.

如果您的数据库中的图像存储为包"而不是长二进制数据",则您需要去除 MS Access 添加的标题信息.我一直在使用简单的 .jpg 文件使用包"类型的图像存储.在这种情况下,标头比 78 字节长得多.在这个例子中,它实际上是234个字节,MS Access还在原始文件的末尾添加了一些信息;在这种情况下大约 292 个字节.

If the image in your database is stored as a 'Package' and not a 'Long binary data', then you will need to strip the header information that MS Access adds. I've been playing with the 'Package' type of image storage with a simple .jpg file. The header in this case is much longer than 78 bytes. In this instance, it's actually 234 bytes, and MS Access also added some information to the end of the original file; about 292 bytes in this case.

看起来您的原始方法是正确的,您只需要根据您的情况确定要从 Byte 数组的前部和后部剥离多少字节.

It looks like your original approach was correct, you will just need to determine how many bytes to strip off the front and rear of the Byte array for your situation.

我通过在十六进制编辑器中比较原始图像文件和从数据库导出的文件(不是流对象,请参阅我的第一个代码)为我的文件确定了它.一旦我弄清楚 MS Access 添加了多少信息(页眉和页脚),我就知道需要删除多少字节.

I determined it for my file by comparing the original image file, and the file exported from the database, (not to a Stream object, see my first code) in a hex editor. Once I figured out how much information (header and footer) was added by MS Access, I then knew how many bytes needed to be stripped.

当图像存储为包"时,MS Access 添加的标题大小取决于文件类型,以及图像在转储到 MS Access 数据库时的原始位置(完整路径信息).因此,即使对于相同的文件类型,您可能需要从每个文件的标头中去除不同数量的字节.这使它变得更加困难,因为您将不得不扫描字节数组,直到找到该文件类型的正常文件开始信息,然后删除它之前的所有内容.

The size of the header added by MS Access when the image is stored as 'Package' varies depending on the file type, and the original location (full path information) of the image when it was dumped into the MS Access database. So, even for the same file type, you may have a different number of bytes to strip from the header for each file. This makes it a lot more difficult, because then you will have to scan the byte array until you find the normal start-of-file information for that file type, and then strip everything before it.

所有这些令人头疼的问题是最好将图像作为 BLOB长二进制数据"存储在数据库中的原因之一.检索要容易得多.我不知道您是否可以选择这样做,但如果可以,这将是一个好主意.

All this headache is one of the reasons that it is better to store images as BLOBs 'Long binary data' in a database. Retrieval is much easier. I don't know if you have the option to do this, but if so, it would be a good idea.

这篇关于从 Access 读取图像 - 参数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆