从访问数据库中检索图像 [英] Retrieve an image from access database

查看:66
本文介绍了从访问数据库中检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以在此代码中为我提供帮助吗?

Please can anyone help me in this code ??

我正在尝试从Access数据库检索图像.

I am trying to retrieve an image from an Access database..

private void btnRetrieve_Click_1(object sender, EventArgs e)
{
   //Save Temporarily that image bytes in one location and give that path to picture box 
   con.Open();

   cmd = new OleDbCommand("select pic from shapes2 where ID= 1", con);

   da = new OleDbDataAdapter(cmd);
   da.Fill(dt);

   if (dt.Rows.Count > 0)
   {
      if (dt.Rows[0]["pic"] != DBNull.Value)
      {
         pictureBox4.Image = ByteArrayToImage((Byte[])dt.Rows[0]["pic"]);
      }
   }

   con.Close();
}

Bitmap ByteArrayToImage(byte[] b)
{
    MemoryStream ms = new MemoryStream();
    byte[] pData = b;
    ms.Write(pData, 0, Convert.ToInt32(pData.Length));
    Bitmap bm = new Bitmap(ms, false);
    ms.Dispose();
    return bm;
}

推荐答案

这似乎是一个常见问题,请参见讨论:从Access读取图像-参数无效

This seems to be a common issue, see the discussion: Reading image from Access - parameter not valid

问题在于图像可以以不同的格式存储在Access数据库中:二进制blob或作为OLE对象.

The issue is that images can be stored in Access databases in different formats: binary blob or as an OLE object.

我不知道您在Access数据库中使用的是哪个数据库,但是如果它另存为纯二进制二进制文件,则它是图像本身,因此您的代码应该可以使用.

I do not know which one you are using in your Access database, but if it's save as a straight binary blob then it is the image itself, so your code should work.

由于您遇到了问题,这可能意味着图像的存储方式有所不同:作为OLE对象.在这种情况下,还存在与图像数据一起存储的元数据,并且该元数据使获取图像变得更加困难,因为您必须以一种或另一种方式将其剥离.OLE元数据的长度是可变的,因此您不能只是跳过它.
我所看到的最好的方法是通过尝试在OLE Blob中找到它的幻数来查找图像文件的开头.

Since you are having the issue, it probably means that the images were stored differently: as OLE object. In that case there is metadata also stored with the image data and that metadata makes getting to the image more difficult since you have to strip it away one way or another. The OLE metadata is of variable length, so you can't just skip it.
The best way I have seen it done is to find the begining of the image file by trying to find its' magic number in the OLE blob.

以下文章和问题将向您展示如何:

The following articles and question will show you how:

  • Removing OLE Header from images stored in MS Access DB as OLE Object
  • Extract OLE Object (pdf) from Access DB (similar technique that you can adapt to jpg/gif/etc images too)

这篇关于从访问数据库中检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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