Kamis, 24 Maret 2016

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

insert image to datagridview using picturebox

VB.NET - How To Insert Images Into DataGridView From PictureBox In VBNET

                                                                                                                         

In This VB.NET Tutorial We Will See How To Browse And Display Image Into PictureBox In VB.NET Programming Language.


Project Source Code:

Imports System.IO

Public Class Add_Image_To_DataGridView_From_PictureBox

   
    Private Sub Add_Image_To_DataGridView_From_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"
        dgvImageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch

        Dim dgvTextColumn As New DataGridViewTextBoxColumn
        dgvTextColumn.HeaderText = "Id"

        DataGridView1.Columns.Add(dgvTextColumn)
        DataGridView1.Columns.Add(dgvImageColumn)

        DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
        DataGridView1.RowTemplate.Height = 120
        DataGridView1.AllowUserToAddRows = False

    End Sub
    ' button load image into picturebox
    Private Sub BtnBrowseImage_Click(sender As Object, e As EventArgs) Handles BtnBrowseImage.Click
        Dim opf As New OpenFileDialog
        opf.Filter = "Choose Image(*.jpg;*.png;*.gif)|*.jpg;*.png;*.gif"

        If opf.ShowDialog = DialogResult.OK Then
            PictureBox1.Image = Image.FromFile(opf.FileName)
        End If
    End Sub

    ' button add image
    Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click

        Try
            Dim ms As New MemoryStream
            PictureBox1.Image.Save(MS, PictureBox1.Image.RawFormat)
            Dim img As Byte()
            img = ms.ToArray()
            DataGridView1.Rows.Add(TextBox1.Text, img)
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
        End Try

    End Sub
End Class

///////////////OUTPUT:


Vb net insert image to datagridview using picturebox
Vb net insert image to datagridview using picturebox

Bagikan

Jangan lewatkan

VB NET - How To Add Image To DataGridView From PictureBox Using VB.NET
4/ 5
Oleh

Subscribe via email

Suka dengan artikel di atas? Tambahkan email Anda untuk berlangganan.