Reputation: 1
Can you tell me how I can to retrieve an image from a field (type oleobject) named IMGFAM in a MSACCESS table to a group (many as records in a table) of pictureboxes at runtime.
I can display buttons but I can't do that with the picture (jpg) obtained from the field. Thanks for your help.
P.S: I used MS Access 2007 and VB.net 2008 express.
Upvotes: 0
Views: 7050
Reputation: 1
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]
Upvotes: 0
Reputation: 96
Before you want to manipulate image to db Access, you must to know basic class of IO.MemoryStream. Below Sample code from vb forum 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
Upvotes: 2