0

你能告诉我如何在运行时从 MSACCESS 表中名为 IMGFAM 的字段(类型 oleobject)检索图像到一组图片框(许多作为表中的记录)。

我可以显示按钮,但我不能用从现场获得的图片(jpg)来做到这一点。谢谢你的帮助。

PS:我使用了 MS Access 2007 和 VB.net 2008 express。

4

2 回答 2

2

在你想将图像操作到 db Access 之前,你必须了解 IO.MemoryStream 的基本类。下面来自 vb 论坛的示例代码http://www.vbforums.com/showthread.php?489787-02-03-Loading-JPG-images-from-Access-to-VB.Net

Imports System.Data.OleDb
Imports System.IO

Public Class Form1

Private connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\db1.mdb;User Id=admin;Password=;")

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.PictureBox1.Image = Image.FromFile(Path.Combine(Application.StartupPath, "Properties.jpg"))
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
        Dim stream As New IO.MemoryStream

        Me.PictureBox1.Image.Save(stream, Imaging.ImageFormat.Jpeg)

        Dim command As New OleDbCommand("INSERT INTO Table1 (Picture) VALUES (@Picture)", connection)

        command.Parameters.AddWithValue("@Picture", stream.GetBuffer())

        connection.Open()
        command.ExecuteNonQuery()
        connection.Close()
        command.Dispose()
        stream.Dispose()
    Catch ex As Exception
        MessageBox.Show(ex.ToString())
    End Try
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Try
        Dim command As New OleDbCommand("SELECT Picture FROM Table1 WHERE ID = @ID", connection)

        Me.connection.Open()
        command.Parameters.AddWithValue("@ID", Me.GetGreatestID())

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

        connection.Close()
        command.Dispose()

        Dim stream As New IO.MemoryStream(pictureData)

        Me.PictureBox2.Image = Image.FromStream(stream)
        stream.Dispose()
    Catch ex As Exception
        MessageBox.Show(ex.ToString())
    End Try
End Sub
于 2013-02-27T09:10:08.747 回答
0
Dim OpenFileDialog1 As New OpenFileDialog
        With OpenFileDialog1

            .CheckFileExists = True

            .ShowReadOnly = False

            .Filter = "All Files|*.*|Bitmap Files (*)|*.bmp;*.gif;*.jpg"

            .FilterIndex = 2

            If .ShowDialog = DialogResult.OK Then

                ' Load the specified file into a PictureBox control.

                pbNewImage.Image = Image.FromFile(.FileName)

            End If

for detail one can visit my blog: [enter link description here][1]
于 2013-03-10T04:58:52.853 回答