将Access数据库中存储的图片加载到程序VB.NET中 [英] Loading Picture Stored in Access Database into Program VB.NET

查看:231
本文介绍了将Access数据库中存储的图片加载到程序VB.NET中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过数据源与VB项目链接的Access数据库。在其中一个表的数据库中,我有一个OLE对象字段。我已在此字段中以.BMP格式和.JPG格式保存图片。我遇到的问题是将此图像加载到我的应用程序中。这是我希望能够做到的:



 ButtonMeal1.BackgroundImage = IPOSDBDataSet.Meals.Rows( 0 )。项目( 5 





其中Item(5)是存储图像的行的列。但是,我知道这不起作用。



我也试过这种方法:



< pre lang =vb> Dim ImageByteArray As 字节()= CType (IPOSDBDataSet.Meals.Rows( 0 )。Item( 5 ),字节())
Dim ImageMemoryStream As MemoryStream = IO.MemoryStream(ImageByteArray)
Dim MyImage As Image = Drawing.Image.FromStream(ImageMemoryStream)
PictureBox1.Image = MyImage





但是,当我调试这个时,我收到错误参数无效。我已经多次修改了这个,但是没有任何好的结果。



我还读过关于存储在OLE Object字段中的标题的东西,它必须是删除了,但我不知道怎么做!关于我的问题,互联网上没有明确的答案。最终,我希望能够以任何可能的方式从数据库中将图像加载到我的程序中!



请帮忙!

解决方案

使用Northwind.sdf文件时我碰到了OLE图像格式。奇怪的是,DataGridView Image列可以处理这些数据,但Image Class不能。



我修改了本文中的代码( http://blogs.msdn.com/b/pranab/archive/2008/07/15/removing-ole-header-from-images-stored-in-ms-access-db-as-ole-object.aspx [ ^ ])从函数中删除字节数组中的OLE数据。



修改代码的这一行以使用该函数。



 Dim ImageMemoryStream作为MemoryStream = New IO。 MemoryStream(GetImageBytesFromOLEField(ImageByteArray))



 朋友 共享 函数 GetImageBytesFromOLEField( ByVal  oleFieldBytes() As  字节作为 字节()

Dim BITMAP_ID_BLOCK 作为 字符串 = BM
Dim JPG_ID_BLOCK As String = ChrW(& HFF).ToString()& ChrW(& HD8).ToString()& ChrW(& HFF).ToString()
Dim PNG_ID_BLOCK As String = ChrW(& H89).ToString()& PNG& vbCrLf& ChrW(& H1A).ToString()& vbLf
Dim GIF_ID_BLOCK As String = GIF8
Dim TIFF_ID_BLOCK 作为 字符串 = < span class =code-string> II *& ChrW(& H0).ToString()

Dim imageBytes() As 字节

' 获取UTF7编码字符串版本
Dim u7 As System.Text.Encoding = System.Text.Encoding.UTF7
Dim strTemp As String = u7.GetString(oleFieldBytes)
Dim st2 As < span class =code-keyword> String = System.Text.Encoding.UTF8.GetString(oleFieldBytes)
' 从字符串中获取前300个字符
Dim strVTemp As String = strTemp.Substring( 0 300

' 搜索块
Dim iPos 作为 整数 = -1
如果 strVTemp.IndexOf(BITMAP_ID_BLOCK)<> -1 然后
iPos = strVTemp.IndexOf(BITMAP_ID_BLOCK)
ElseIf strVTemp。 IndexOf(JPG_ID_BLOCK)<> -1 然后
iPos = strVTemp.IndexOf(JPG_ID_BLOCK)
ElseIf strVTemp。 IndexOf(PNG_ID_BLOCK)<> -1 然后
iPos = strVTemp.IndexOf(PNG_ID_BLOCK)
ElseIf strVTemp。 IndexOf(GIF_ID_BLOCK)<> -1 然后
iPos = strVTemp.IndexOf(GIF_ID_BLOCK)
ElseIf strVTemp。 IndexOf(TIFF_ID_BLOCK)<> -1 然后
iPos = strVTemp.IndexOf(TIFF_ID_BLOCK)
否则
抛出 异常( < span class =code-string>无法确定OLE对象的标题大小)
结束 如果


' 从上面的位置获取新图片
如果 iPos = -1 那么
< span class =code-keyword>抛出 异常( 无法确定OLE对象的标题大小
结束 如果

imageBytes = 字节 CInt (oleFieldBytes.LongLength - iPos - 1 )){}
Array.ConstrainedCopy(oleFieldBytes,iPos,imageBytes, 0 , oleFieldBytes.Length - iPos)

返回 imageBytes

结束 功能


I have an Access Database linked with a VB project through a data source. In the database on one of the tables, I have an OLE Object field. I have saved pictures in .BMP format and .JPG format in this field. The problem I am encountering is loading this image into my application. This is what I would like to be able to do:

ButtonMeal1.BackgroundImage = IPOSDBDataSet.Meals.Rows(0).Item(5)



Where Item(5) is the column of the row where the image is stored. However, I know that this does not work.

I have also tried this method:

Dim ImageByteArray As Byte() = CType(IPOSDBDataSet.Meals.Rows(0).Item(5), Byte())
Dim ImageMemoryStream As MemoryStream = New IO.MemoryStream(ImageByteArray)
Dim MyImage As Image = Drawing.Image.FromStream(ImageMemoryStream)
PictureBox1.Image = MyImage



However, when I debug this, I get the error "Parameter is not valid". I have modified this multiple times, but without any good result.

I have also read stuff about a header being stored in the OLE Object field, and that it must be removed, but I have no idea how to do this! There are no clear answers on the internet regarding my issue. Ultimately, I would just like to be able to load an image into my program from a database, whatever way possible!

Please help!

解决方案

I bumped into the OLE image format when using the Northwind.sdf file. The strange thing is that the DataGridView Image column can handle this data, but the Image Class can not.

I modified the code from this article (http://blogs.msdn.com/b/pranab/archive/2008/07/15/removing-ole-header-from-images-stored-in-ms-access-db-as-ole-object.aspx[^]) into a function to strip out the OLE data from a byte array.

Modify this line of your code to use the function.

Dim ImageMemoryStream As MemoryStream = New IO.MemoryStream(GetImageBytesFromOLEField(ImageByteArray))


Friend Shared Function GetImageBytesFromOLEField(ByVal oleFieldBytes() As Byte) As Byte()

   Dim BITMAP_ID_BLOCK As String = "BM"
   Dim JPG_ID_BLOCK As String = ChrW(&HFF).ToString() & ChrW(&HD8).ToString() & ChrW(&HFF).ToString()
   Dim PNG_ID_BLOCK As String = ChrW(&H89).ToString() & "PNG" & vbCrLf & ChrW(&H1A).ToString() & vbLf
   Dim GIF_ID_BLOCK As String = "GIF8"
   Dim TIFF_ID_BLOCK As String = "II*" & ChrW(&H0).ToString()

   Dim imageBytes() As Byte

   ' Get a UTF7 Encoded string version
   Dim u7 As System.Text.Encoding = System.Text.Encoding.UTF7
   Dim strTemp As String = u7.GetString(oleFieldBytes)
   Dim st2 As String = System.Text.Encoding.UTF8.GetString(oleFieldBytes)
   ' Get the first 300 characters from the string
   Dim strVTemp As String = strTemp.Substring(0, 300)

   ' Search for the block
   Dim iPos As Integer = -1
   If strVTemp.IndexOf(BITMAP_ID_BLOCK) <> -1 Then
      iPos = strVTemp.IndexOf(BITMAP_ID_BLOCK)
   ElseIf strVTemp.IndexOf(JPG_ID_BLOCK) <> -1 Then
      iPos = strVTemp.IndexOf(JPG_ID_BLOCK)
   ElseIf strVTemp.IndexOf(PNG_ID_BLOCK) <> -1 Then
      iPos = strVTemp.IndexOf(PNG_ID_BLOCK)
   ElseIf strVTemp.IndexOf(GIF_ID_BLOCK) <> -1 Then
      iPos = strVTemp.IndexOf(GIF_ID_BLOCK)
   ElseIf strVTemp.IndexOf(TIFF_ID_BLOCK) <> -1 Then
      iPos = strVTemp.IndexOf(TIFF_ID_BLOCK)
   Else
      Throw New Exception("Unable to determine header size for the OLE Object")
   End If


   ' From the position above get the new image
   If iPos = -1 Then
   Throw New Exception("Unable to determine header size for the OLE Object")
   End If

   imageBytes = New Byte(CInt(oleFieldBytes.LongLength - iPos - 1)) {}
   Array.ConstrainedCopy(oleFieldBytes, iPos, imageBytes, 0, oleFieldBytes.Length - iPos)

   Return imageBytes

End Function


这篇关于将Access数据库中存储的图片加载到程序VB.NET中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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