0

実行時に、MSACCESS テーブルの IMGFAM という名前のフィールド (oleobject 型) からピクチャ ボックスのグループ (テーブル内の多くのレコード) に画像を取得する方法を教えてください。

ボタンを表示できますが、フィールドから取得した画像 (jpg) では表示できません。ご協力いただきありがとうございます。

PS: MS Access 2007 と VB.net 2008 Express を使用しました。

4

2 に答える 2

2

イメージをdbAccessに操作する前に、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 に答える