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

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

问题描述

我在访问.mdb文件中简单的数据库,但我不知道该如何处理:参数无效异常时,我可是从数据流生成图片
我心中已经阅读我需要剥去78字节偏移(的nofollow的>),但我的还是获得了参数无效错误
当我打电话FromStream,甚至剥离的第78个字节后。






这不为我工作:

 字节[] = abytPic(字节[])dt.Rows [0] [照片];字节arrary与图像
如果((abytPic [0] == 21)及及(abytPic [1] == 28))//这是真的
{
的byte [] abytStripped =新的字节[abytPic.Length - 78]。
System.Buffer.BlockCopy(abytPic,78,abytStripped,0,abytPic.Length - 78);
msPic =新emoryStream(abytStripped);
}


解决方案

如果你正在读数据直接从MS Access,你不需要任何头信息剥离。



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

 暗淡varBytes()作为字节

使用CN作为新的OleDbConnection(myConnectionString)
cn.Open()

SQLTEXT =SELECT [myColumn]_
和; FROM [mytable的]_
和; WHE​​RE([mySearchCriteria] ='&放大器; mySearchTerm&安培;')

使用厘米作为新的OleDbCommand(SQLTEXT,CN)
昏暗RDR作为OleDbDataReader

RDR = cm.ExecuteReader

rdr.Read()

varBytes = rdr.GetValue(0)
端使用

使用完

My.Computer.FileSystem.WriteAllBytes(mypath中&安培;\myFile.emf,varBytes,真)

这是我周围铺设这个例子之一,我知道文件在数据库中的.emf图像。如果您知道分机,你可以把它放在文件名。如果你不这样做,你可以将它留空,然后打开带有图像查看器所产生的;应开始。如果你需要找到扩展名或文件类型,一旦它被保存为一个文件,你可以用任何十六进制编辑器打开它,文件类型将可以从标题信息。



你的问题是有点不清楚,所以我不知道上面的代码正是你想要的,但它应该让你拉近了许多。



修改



这是VB代码,采用字节数组,加载到一个MemoryStream对象,然后创建一个Image对象从Stream。这段代码工作得很好,在我的窗体上的图片框显示的图像。

 昏暗IMG作为图像
尺寸str作为新的MemoryStream(varBytes)
IMG = Image.FromStream(STR)
PictureBox1.Image = IMG

如果在C#相当于这不是为你工作,那么问题很可能在图像是如何存储在MS Access数据库。



编辑:



如果在数据库中的图像保存为一个'包',而不是'长二进制数据,然后你将需要剥离的MS Access将添加的头信息。我一直在玩图像存储与一个简单的.jpg文件的一揽子的类型。在这种情况下,首标是超过78字节长得多。在这种情况下,它实际上是234个字节,和MS Access还增加了一些信息,以原来的文件的结束;大约292个字节在这种情况下。



它看起来像你原来的做法是正确的,你只需要确定多少字节剥去字节的前部和后部阵列您的具体情况。



我决定为我的文件通过比较原始图像文件,并从数据库导出的文件,(而不是一个Stream对象,看我的第一个代码)的十六进制编辑器。一旦我想通了信息多少(页眉和页脚)由MS访问加,然后我就知道需要多少字节被剥离



编辑:< /强>



由MS访问时添加取决于该文件类型的图像被存储为包的头的大小,和原来的位置(全路径图像信息),当它被倾倒入MS Access数据库。所以,即使是相同的文件类型,则可能有不同数目的字节,从每个文件的头剥离。
这使得它困难得多,因为你将要扫描的字节数组,直到找到启动的文件正常信息的文件类型,然后才剥夺了一切。



这一切是头痛的原因,这是更好地存储图像作为数据库中的BLOB长二进制数据之一。检索要容易得多。我不知道你是否有这样做的选项,但如果是这样,这将是一个不错的主意。


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.


This doesn't work for me:

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

解决方案

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

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)

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.

EDIT:

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

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.

EDIT:

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.

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.

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.

EDIT:

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.

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天全站免登陆