使用 Access & 存储/检索图像VB.NET 2012 [英] Storing / Retrieving Images with Access & VB.NET 2012

查看:33
本文介绍了使用 Access & 存储/检索图像VB.NET 2012的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VB.NET 2012

VB.NET 2012

我有一个图像,一个 mdb 文件,而且头疼...

I have an image, an mdb file, and a headache...

我创建了一个包含路径字段、名称字段和对象字段的表,用于在 Access 中存储图像.很容易执行:

I've created a table that will have a path field, name field, and object field to store an image in Access. Easy enough to execute:

CREATE TABLE DATATABLE (FilePath CHAR, FileName CHAR, FileObject LONGBINARY)

我现在的问题?以某种方式将图像放入和取出该桌子.这似乎应该很容易完成 - 但我一直在用过时的 Append/GetChunk 方法、不起作用的 WriteBLOBToFile 方法以及其他所有方法来解决问题.Google 通常是我的朋友,但今天我得到的帮助却不起作用.

My problem now? SOMEHOW getting an image into, and out of, that table. This seems like it should be easily done - but I've been banging my head on the table with outdated Append/GetChunk methods, WriteBLOBToFile methods that don't work, and everything else. Google is usually my friend, but today I'm getting help that just doesn't work.

有没有人有办法将图像放入和取出上述表格?这看起来真的应该是直截了当的.

Anyone out there have a way to get an image into, and out of, the above-mentioned table? This really seems like it should be straight-forward.

请 - 尽管我更愿意将图像存储在文件系统中而不是这个,但规范要求在 Access 中存储图像.没办法.感谢您对此的任何帮助!!!

And please - as much as I'd rather store the images in the file system rather than this, the specs are calling for image storage in Access. No way around it. Thanks for any help on this!!!

谢谢,

杰森

推荐答案

根据建议,如果您不需要存储数据,则可以使用 MemoryStream 作为中介在 Access 中作为图像访问.

As suggested, you can use a MemoryStream as an intermediary if you don't need to store the data such that it can be accessed as an image in Access.

Private Sub SaveImage(picture As Image, id As Integer)
    Dim connection As New OleDbConnection("connection string here")
    Dim command As New OleDbCommand("UPDATE MyTable SET Picture = @Picture WHERE ID = @ID", connection)

    'Create an empty stream in memory.
    Using stream As New IO.MemoryStream
        'Fill the stream with the binary data from the Image.
        picture.Save(stream, Imaging.ImageFormat.Jpeg)

        'Get an array of Bytes from the stream and assign to the parameter.
        command.Parameters.AddWithValue("@Picture", stream.GetBuffer())
    End Using

    command.Parameters.AddWithValue("@ID", id)

    connection.Open()
    command.ExecuteNonQuery()
    connection.Close()
End Sub

Private Function LoadImage(id As Integer) As Image
    Dim connection As New OleDbConnection("connection string here")
    Dim command As New OleDbCommand("SELECT Picture FROM MyTable WHERE ID = @ID", connection)

    command.Parameters.AddWithValue("@ID", id)

    connection.Open()

    Dim pictureData As Byte() = DirectCast(command.ExecuteScalar(), Byte())

    connection.Close()

    Dim picture As Image

    'Create a stream in memory containing the bytes that comprise the image.
    Using stream As New IO.MemoryStream(pictureData)
        'Read the stream and create an Image object from the data.
        picture = Image.FromStream(stream)
    End Using

    Return picture
End Function

这篇关于使用 Access & 存储/检索图像VB.NET 2012的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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