2013-02-27 71 views
0

你能告訴我怎麼可以在一個MSACCESS表中檢索從字段(類型OLEOBJECT)圖像命名IMGFAM一組(多達表中的記錄),在運行時pictureboxes的。圖片來自MSACCESS表

我可以顯示按鈕,但我不能做到這一點與從現場獲得的畫面(JPG)。謝謝你的幫助。

P.S:我使用的MS Access 2007年和2008年VB.net明確。

回答

2

之前要處理圖像數據庫訪問,你必須瞭解基本的類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 
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] 
示例代碼