using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
//------------------------------------
using System.IO;
using System.Data.SqlClient;

namespace _17_9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string strConn = "Server=.;" + "DataBase=Pictures;" +
                         "Integrated Security=True";

        OpenFileDialog ofd1 = new OpenFileDialog();

        private Image ByteToImage(byte[] arrImage)
        {
            MemoryStream ms = new MemoryStream(arrImage);
            Image image = Image.FromStream(ms);
            ms.Close();

            return image;
        }

        private byte[] ImageToByte(string sPath)
        {
            MemoryStream ms = new MemoryStream();
            pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
            byte[] arrImage = ms.GetBuffer();
            ms.Close();

            return arrImage;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.BackColor = Color.LightBlue;
            pictureBox1.BorderStyle = BorderStyle.Fixed3D;
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

            ofd1.Title = "Select Picture";
            ofd1.Filter = "Picture Files (*.jpg)|*.jpg|All Files (*.*)|*.*";
            ofd1.FileName = string.Empty;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ofd1.FileName = string.Empty;

            if (ofd1.ShowDialog() == DialogResult.OK)
            {
                Image image;
                string fileName = ofd1.FileName;
                image = new Bitmap(fileName);
                pictureBox1.Image = (Image)image;
                pictureBox1.Refresh();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image != null)
            {
                try
                {
                    SqlConnection sCon = new SqlConnection(strConn);
                    SqlCommand sc = new SqlCommand();
                    sc.Connection = sCon;
                    sCon.Open();
                    sc.CommandType = CommandType.Text;
                    sc.CommandText = "Insert Into tblPic(Pic) Values(@Pic)";
                    sc.Parameters.Add("@Pic", SqlDbType.VarBinary);
                    sc.Parameters["@Pic"].Value = ImageToByte(ofd1.FileName);
                    sc.ExecuteNonQuery();
                    sCon.Close();

                    MessageBox.Show("تصویر در پایگاه داده ذخیره شد");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("لطفا یک تصویر انتخاب کنید");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == string.Empty)
            {
                MessageBox.Show("لطفا کد تصویر را وارد کنید");
                textBox1.Focus();

                return;
            }

            SqlConnection sCon = new SqlConnection(strConn);
            SqlCommand sc = new SqlCommand();
            sc.Connection = sCon;
            sCon.Open();
            sc.CommandType = CommandType.Text;
            sc.CommandText = "Select Count(*) From tblPic Where ID=@ID";
            sc.Parameters.AddWithValue("@ID", textBox1.Text);
            
            int count = (int)sc.ExecuteScalar();

            if (count < 1)
            {
                MessageBox.Show("کد وارد شده صحیح نیست");
                sCon.Close();

                return;
            }

            sc.CommandType = CommandType.Text;
            sc.CommandText = "Select Pic From tblPic Where ID=@ID";
            pictureBox1.Image = ByteToImage((byte[])sc.ExecuteScalar());
            sCon.Close();
        }
    }
}