Image Column in Windows Application

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • krishnaneeraja
    New Member
    • Mar 2008
    • 21

    Image Column in Windows Application

    Hi,
    Iam working on windows based application.In that i dont know how to add image in grid view column. I want to add image column in grid view.Image path will be stored in Ms-Access database.I want to retrieve path from database and place image in gridview column.

    Please help me.

    Thanks & Regards
    Neeraja.
  • Prathap
    New Member
    • Nov 2011
    • 37

    #2
    Here i've used picturebox.
    Code:
    Imports System.Data.SqlClient
    Imports System.Data
    Imports System.IO
     
     Public Class Form1
        Dim str As String = "Data Source=NET3\SQLEXPRESS;Initial Catalog=RestPos;Persist Security Info=True;User ID=sa;Password=password"
        Dim con As New SqlClient.SqlConnection
    
        'To open an image from computer
        '-------------------------------
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            OpenFileDialog1.Title = "Please select a file"
            OpenFileDialog1.InitialDirectory = "c:temp"
            OpenFileDialog1.ShowDialog()
            TextBox2.Text = OpenFileDialog1.FileName.ToString  '--->To Show the file path in textbox2
            PictureBox1.ImageLocation = TextBox2.Text  '--->To show selected image in picturebox
        End sub
       
       'To insert selected image into database
       'The datatype of the column in table to store image should be <image>  
       '--------------------------------------------------------------------
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            con.ConnectionString = str
            Dim ms As New IO.MemoryStream()
            PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
            Dim arrimage() As Byte = ms.GetBuffer
            Dim cmd As New SqlCommand("insert into image (Emp_Image)values(@picture)", con)
            cmd.Parameters.Add(New SqlParameter("@Picture", SqlDbType.Image)).Value = arrimage
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
        End Sub
        
        'We have successfully inserted the image into database.
        'Now we want to Retrieve the image from database.
        '-------------------------------------------------
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            Dim stream As New IO.MemoryStream()
            con.Open()
            Dim command As New SqlCommand("select Emp_Image from Image where Emp_Id='" + TextBox3.Text + "'", con) '--->You can give Emp_id instead of Textbox value.
            Dim image As Byte() = DirectCast(command.ExecuteScalar(), Byte())
            stream.Write(image, 0, image.Length)
            con.Close()
            Dim bitmap As New Bitmap(stream)
            PictureBox2.Image = bitmap '--->I have used another picturebox to display image from database.
        End Sub
        'Thats all, You can place a linklabel below the picturebox if you to change photo and update it in database.
    Regards,
    Prathap

    Comment

    Working...