Save Image

Design


    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.Data.OleDb;
    using System.IO;
    
    namespace WindowsFormsApplication17
    {
        public partial class Form1 : Form
        {
            OleDbCommand cmd = new OleDbCommand();
            OleDbConnection con = new OleDbConnection();
    
            OpenFileDialog ofd = new OpenFileDialog();
            String Khan;

            public Form1()
            {
                InitializeComponent();
                cmd.Connection = con;
                con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\AIT-Lab-3\Documents\numani.accdb";
            }
    
            private void  button2_Click(object sender, EventArgs e)
            {
                OleDbConnection con = new OleDbConnection();
                OleDbCommand cmd = new OleDbCommand();

                cmd.CommandText = "Select * From Where ID Like '% " + textBox1.Text + "%'";

                OleDbDataReader dr;
                dr = cmd.ExecuteReader();

                if(dr.Read())
                {
                    textBox1.Text = dr["ID"].ToString();
                    Bitmap bm = BytesToImage((byte[])dr["Img"]);
                    pictureBox1.Image = bm;

                }
            }
    
            private   button3_Click (object sender, EventArgs e)
            {
                ofd.ValidateNames = true;
                if(ofd.ShowDialog()==DialogResult.OK)
                {
                    pictureBox1.Image = Image.FromFile(ofd.FileName);
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                // TODO: This line of code loads data into the 'numaniDataSet.Numi' table. You can move, or remove it, as needed.
                this.numiTableAdapter.Fill(this.numaniDataSet.Numi);
            }
            
            private void saveImage (String Shaikh)
            {
                try
                {
                    con.Open();
                    String path = ofd.FileName;
                    byte[] imagedata;
    
                    cmd = new OleDbCommand();
                    cmd.Connection = con;
                    cmd.CommandText = Shaikh;
                    imagedata = System.IO.File.ReadAllBytes(@path);
                    cmd.Parameters.AddWithValue("@IM", imagedata);
                    cmd.Parameters.AddWithValue("@ID", textBox1.Text);
                    cmd.ExecuteNonQuery();
    
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    con.Close();
    
                }
            }

              private BytesToImage(byte[]bytes)
            {
                using (MemoryStream img_Stream = new MemoryStream (bytes))
                {
                    Bitmap bm = new Bitmap(img_Stream);
                    img_Stream.Close();
                    return bm;
    
                }
            }
    
            private void  button1_Click(object sender, EventArgs e)
            {
                Khan = "Insert Into Numi(Img) Values (@Img)";
                saveImage(Khan);
                MessageBox.Show("Image_Save");
            }
        }
    }          
                

Output