Click here to Skip to main content
15,843,145 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As of right now I have been trying to arrange my pictures that are in byte for as an array. I want the selected pictures to be added to the same row in a horizontal fashion. Problem is when I set this up as an array the images are added vertically, 1 per row. I need to have all images I select to be added to the same row. The column itself does not need a name.

Imports System.IO

Public Class Form1
    Dim ofd As New OpenFileDialog With {.Filter = "Images|*.jpg;*.bmp;*.png;*.gif;*.wmf"}
    Dim pic As PictureBox
    Dim wid As Int32

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'this portion of code allows users to browse for images and add them into the group box. 
        ofd.Multiselect = True
        If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            For Each t In ofd.FileNames
                pic = New PictureBox
                pic.Image = Image.FromFile(t)
                pic.SizeMode = PictureBoxSizeMode.StretchImage
                pic.SetBounds(wid, 20, 200, 100)
                wid += 205
                AddHandler pic.Click, AddressOf convertPic
                Me.Panel1.Controls.Add(pic)

                ' if you want to add the images straight to the form use: Me.Controls.Add(pic)

                ' this portion of code attaches the image to the picturebox
                PictureBox1.Image = pic.Image
                PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

                ' This portion of code allows you to add pictures to the datagrid. you can use the same code to add into an sql statement.
                Using ms As MemoryStream = New MemoryStream()

                    Dim bm As Bitmap = New Bitmap(PictureBox1.Image)
                    bm.Save(ms, PictureBox1.Image.RawFormat)

                    Dim arrPic() As Byte = ms.GetBuffer()


                    DataGridView1.Columns.Add("", "")
                    DataGridView1.Rows.Add(arrPic.ToArray)


                End Using

            Next

        End If
    End Sub


What I have tried:

I have tried to make an array list:
Dim row As New ArrayList
                   row.Add(arrPic.ToArray)
                   DataGridView1.Rows.Add(row)


but when I try it this way the byte is converted to s systems string and cannot be opened up.
Posted
Updated 22-Mar-17 11:45am
v2

1 solution

If you call Rows.Add for each picture, it shouldn't come as a surprise when your code adds a new row for each picture. :)

If you want to add all of the pictures as columns to a single row, then you'll need to add a single row, and add the images to that row.
VB.NET
If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
    Dim fileCount = ofd.FileNames.Length
    Dim images(fileCount - 1) As Image
    
    For i As Integer = 0 To fileCount - 1
        If i >= DataGridView1.Columns.Count Then
            Dim col As New DataGridViewImageColumn()
            col.Width = 200
            DataGridView1.Columns.Add(col)
        End If
        
        images(i) = Image.FromFile(ofd.FileNames(i))
        
        Dim pic As New PictureBox()
        pic.Image = images(i)
        pic.SizeMode = PictureBoxSizeMode.StretchImage
        pic.SetBounds(wid, 20, 200, 100)
        AddHandler pic.Click, AddressOf convertPic
        Me.Panel1.Controls.Add(pic)
        wid += 205
    Next
    
    ' NB: No point doing this in the loop, as only the last one will apply:
    PictureBox1.Image = images(fileCount - 1)
    PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    
    DataGridView1.Rows.Add(images)
End If
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900