How can I DISPLAY AN IMAGE into a PICTUREBOX when a particular row is clicked on a LISTVIEW.

  • Thread starter Thread starter help for my project
  • Start date Start date
H

help for my project

Guest
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using WinFormCharpWebCam;
using System.Data.SqlClient;



namespace Camera_Access_Demo
{
public partial class Form1 : Form
{

static String conString = @"Data Source=desktop-jl4hr7d;Initial Catalog=WebcamDB;Integrated Security=True";
SqlConnection con = new SqlConnection(conString);
SqlCommand cmd;
SqlDataAdapter adapter;
DataTable dt = new DataTable();
WebCam webcam;
public Form1()
{
InitializeComponent();

listView1.Columns.Add("ID", 70);
listView1.Columns.Add("Sname", 150);
listView1.Columns.Add("fname", 150);
listView1.Columns.Add("DOB", 150);
listView1.Columns.Add("class", 150);
listView1.Columns.Add("contact", 150);
listView1.Columns.Add("address", 150);
listView1.Columns.Add("image", 150);

listView1.View = View.Details;
listView1.FullRowSelect = true;

}

private void populateLV(String id, String sname, String fname, String dob, String classs, String contact, String address, String image)
{
String[] row = { id, sname, fname, dob, classs, contact, address, image };

listView1.Items.Add(new ListViewItem(row));
}

private void Form1_Load(object sender, EventArgs e)
{
retrieve();


webcam = new WebCam();
webcam.InitializeWebCam(ref pictureBox1);
webcam.Start();
//SqlDataAdapter da = new SqlDataAdapter("select * from Cam_DB order by id desc ", con);
//SqlCommandBuilder cb = new SqlCommandBuilder(da);
//DataTable dt = new DataTable();
//da.Fill(dt);
//dataGridView1.DataSource = dt;
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox2.Image = pictureBox1.Image;
button2.Enabled = true;
}

private void button2_Click(object sender, EventArgs e)
{
try
{
con.Open();

cmd = new SqlCommand("insert into Cam_DB ( sanme, fname, dob, class, contact, address, image)values('" + textBox1.Text + "','" + textBox2.Text + "','" + dateTimePicker1.Value.ToString("MM/dd/yyyy") + "','" + comboBox1.Text + "','" + textBox3.Text + "','" + textBox4.Text + "', @pic)", con);

MemoryStream stream = new MemoryStream();

pictureBox2.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] pic = stream.ToArray();
pictureBox2.Image = Image.FromStream(stream);
cmd.Parameters.AddWithValue("@Pic", pic);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Data Saved!");

retrieve();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void retrieve()
{
listView1.Items.Clear();

String sql = "SELECT * FROM cam_DB";
cmd = new SqlCommand(sql, con);

try
{
con.Open();

adapter = new SqlDataAdapter(cmd);
adapter.Fill(dt);

foreach (DataRow row in dt.Rows)
{
populateLV(row[0].ToString(), row[1].ToString(), row[2].ToString(), row[3].ToString(), row[4].ToString(), row[5].ToString(), row[6].ToString(), row[7].ToString());

}
con.Close();

dt.Rows.Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void listView1_MouseClick(object sender, MouseEventArgs e)
{
String img = listView1.SelectedItems[0].SubItems[7].Text;

pictureBox3.Image = img;

}
}
}

Continue reading...
 
Back
Top