VB.NET - How To Get Image From DataGridView To PictureBox Using VB.NET

Set Image From DataGridView Into PictureBox Using VB.NET

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

        ' 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")

        Dim img2 As Image
        img2 = Image.FromFile("D:\Images\img2.jpg")

        Dim img3 As Image
        img3 = Image.FromFile("D:\Images\img3.jpg")

        Dim img4 As Image
        img4 = Image.FromFile("D:\Images\img4.jpg")

        Dim img5 As Image
        img5 = Image.FromFile("D:\Images\img5.jpg")

    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


Show Images From DataGridView Into PictureBox In VB.NET
Display Images From DataGridView Into PictureBox In VB

Share this

Related Posts

Next Post »