help guys, i need to know how can i add multiple images in a single add button click for example and display it in multiple imageboxes..the project is also associated with an access database that when the user clicks on the button save..all the images inserted will be saved on to database..

Dani AI

Generated

As described, the goal is one Add click that selects multiple files, shows them in separate image boxes, and then saves those images to Access. The hint from to allow multiple selection is the right starting point, and since asked for code, the following shows a compact, practical pattern that covers selection, safe loading (no file locks), dynamic display, and saving.

A practical pattern is: (1) present a file chooser that returns all chosen file names; (2) for each file, read the bytes into memory, create a thumbnail and a dynamic PictureBox (use a FlowLayoutPanel for automatic layout); (3) keep the byte[] (or file path) in an in-memory collection; (4) when Save is clicked, insert those bytes (or the paths) into Access with parameterized commands inside a transaction.

' Add button (VB.NET)
Dim dlg As New OpenFileDialog()
dlg.Filter = "Images|*.jpg;*.jpeg;*.png;*.bmp;*.gif"
dlg.Multiselect = True
If dlg.ShowDialog() = DialogResult.OK Then
    For Each fname As String In dlg.FileNames
        Dim data() As Byte = File.ReadAllBytes(fname)
        Using ms As New MemoryStream(data)
            Dim src As Image = Image.FromStream(ms)
            Dim thumb As New Bitmap(src, New Size(160, 120))
            Dim pb As New PictureBox With {
                .SizeMode = PictureBoxSizeMode.Zoom,
                .Width = 160,
                .Height = 120,
                .Image = New Bitmap(thumb)
            }
            flowLayoutPanel1.Controls.Add(pb)
            imagesToSave.Add(data) ' imagesToSave declared at form scope as List(Of Byte())
        End Using
    Next
End If
' Save button (VB.NET) - insert byte arrays into Access OLE/Attachment column
Using cn As New OleDbConnection(connString)
    cn.Open()
    Dim tr As OleDbTransaction = cn.BeginTransaction()
    Using cmd As New OleDbCommand("INSERT INTO Images (ParentID, ImgData) VALUES (?, ?)", cn)
        cmd.Transaction = tr
        cmd.Parameters.Add("ParentID", OleDbType.Integer)
        cmd.Parameters.Add("ImgData", OleDbType.Binary)
        For Each data() As Byte In imagesToSave
            cmd.Parameters("ParentID").Value = recordId
            cmd.Parameters("ImgData").Value = data
            cmd.ExecuteNonQuery()
        Next
    End Using
    tr.Commit()
End Using

Common pitfalls and notes: Image.FromFile locks files—reading bytes then Image.FromStream and copying to a new Bitmap avoids that. Create thumbnails to limit memory and always Dispose bitmaps no longer needed. Access Attachment fields (2007+) support multiple files per record but are harder to work with programmatically; for larger collections it is often better to save files to disk (unique names) and store paths in the DB. Run heavy saves off the UI thread and wrap DB operations in a transaction to keep data consistent.

Recommended Answers

All 2 Replies

How far you go with this? Post your code.

if you are working common dialog box control then
Try This:-

CommonDialog1.Flags = cdlOFNAllowMultiselect Or cdlOFNLongNames Or cdlOFNExplorer

the above statement will allow to select multiple files.

hope this helps you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.