VB.NET - How To Set Image From DataGridView Into PictureBox In VBNET
In This VB.NET Tutorial We Will See How To Display Images From DataGridView Into PictureBox In VB.NET Programming Language.
Project Source Code:
Imports System.IO
Imports System.Drawing.Imaging
Public Class VB_Get_Image_From_DataGridView_To_PictureBox
Private Sub VB_Get_Image_From_DataGridView_To_PictureBox_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Create Datagridview image column
Dim dgvImageColumn As New DataGridViewImageColumn
' set header text to the column
dgvImageColumn.HeaderText = "Image"
' make the image layout stretch to display the entire image
dgvImageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch
' add the column to the datagridview
DataGridView1.Columns.Add(dgvImageColumn)
' make the columns take all the datagridview width
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
' change the datagridview height
DataGridView1.RowTemplate.Height = 150
DataGridView1.AllowUserToAddRows = False
' create images and add the to the datagridview
Dim img1 As Image
img1 = Image.FromFile("D:\Images\img1.jpg")
DataGridView1.Rows.Add(img1)
Dim img2 As Image
img2 = Image.FromFile("D:\Images\img2.jpg")
DataGridView1.Rows.Add(img2)
Dim img3 As Image
img3 = Image.FromFile("D:\Images\img3.jpg")
DataGridView1.Rows.Add(img3)
Dim img4 As Image
img4 = Image.FromFile("D:\Images\img4.jpg")
DataGridView1.Rows.Add(img4)
Dim img5 As Image
img5 = Image.FromFile("D:\Images\img5.jpg")
DataGridView1.Rows.Add(img5)
End Sub
' DataGridView cell click
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim ms As New MemoryStream
Dim img As Bitmap
img = DataGridView1.CurrentRow.Cells(0).Value
img.Save(ms, ImageFormat.Jpeg)
PictureBox1.Image = Image.FromStream(ms)
End Sub
End Class
///////////////OUTPUT:
Download Projects Source Code