来自MS ACCESS表的PictureBox中的图像

时间:2013-02-27 04:22:05

标签: vb.net picturebox

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

我可以显示按钮但我不能用从该字段获得的图片(jpg)来做。谢谢你的帮助。

P.S:我使用过MS Access 2007和VB.net 2008 express。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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]