Welcome to my website.This website has been designed with you in mind and will allow you to keep up-to-date with my work representing to you.........---Shashi---

Wednesday, 27 April 2011

Save & Retrive image from sql server in C#.net

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;




namespace ImageSave
{
    public partial class Form1 : Form
    {
        SqlConnection conn = new SqlConnection("Server=FSS8;database=abc;integrated security=sspi;");
       
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        public void SaveImage()
        {
            if (txtImageName.Text == "")
            { }
            else
            {
                FileStream fs = new FileStream(txtImageName.Text, FileMode.OpenOrCreate, FileAccess.Read);
                byte[] bt = new byte[fs.Length];
                fs.Read(bt, 0, System.Convert.ToInt32(fs.Length));
                fs.Close();

                SqlCommand cmd = new SqlCommand("Insert into ImageSave(Name,Photo)values(@Name,@Photo)", conn);
                cmd.Parameters.AddWithValue("@Name", txtImageName.Text);
                cmd.Parameters.AddWithValue("@Photo", bt);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show("Image Saved");
                txtImageName.Text = "";
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog opd = new OpenFileDialog();
            opd.Title = "Select Image";
            opd.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
            if (opd.ShowDialog() == DialogResult.OK)
            {
                txtImageName.Text = opd.FileName;
                pictureBox1.Image = Image.FromFile(txtImageName.Text);
                txtImageName.Enabled = false;
            }
        }
        private void btnUpload_Click(object sender, EventArgs e)
        {
            SaveImage();
        }



        private void btnGetImage_Click(object sender, EventArgs e)
        {
            if (txtgetID.Text == "")
            {
                MessageBox.Show("Please Pic ID");
            }
            else
            {
                string cmd = "select Photo from ImageSave where image_id='" + txtgetID.Text + "'";
                SqlCommand com = new SqlCommand(cmd, conn);
                try
                {
                    conn.Open();
                    byte[] b = (byte[])com.ExecuteScalar();
                    MemoryStream mem = new MemoryStream(b);
                    pictureBox1.Image = Image.FromStream(mem);
                    conn.Close();
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.Message); conn.Close();
                }
               
            }
        }
    }
}

No comments:

Post a Comment