Tuesday, 21 August 2012

File Handling in C# ..NET ( How to Save Image Binary Format in Database Table and retrieve it..)


Frist we  create table TableImage in database test and image is saved  in table binary  format………………………………………

use test
Create table TableImage (Id int primary key,Name nvarchar(50),UserImage varbinary(max))



How  to  save  image  in table in Binary Format and how to retrive it in Windows Application
(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.IO;
using System.Data.SqlClient;
namespace FileHandlingExp
{
    public partial class StoreImageInDataBase : Form
    {
        public StoreImageInDataBase()
        {
            InitializeComponent();
        }

        private void StoreImageInDataBase_Load(object sender, EventArgs e)
        {

        }

        object objimage;
        SqlDataReader dr;

//Code  for  select image  from  any Drive……………………………………………

         private void btnSelectImage_Click(object sender, EventArgs e)
            {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtimagesource.Text = openFileDialog1.FileName;
            }
        }

//Code  for  save image in table  in binary format

        private void btnSaveImage_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(txtimagesource.Text, FileMode.Open);
            byte[] data = new byte[fs.Length];
            fs.Read(data, 0, Convert.ToInt32(fs.Length));
            SqlConnection con = new SqlConnection("Data Source=KUSH-PC;Initial Catalog=test;Integrated Security=True");
            SqlCommand com = new SqlCommand("insert into TableImage(Id,Name,UserImage) values(@id,@name,@image)", con);
            com.Parameters.AddWithValue("@id",Convert.ToInt32(txtid.Text));
            com.Parameters.AddWithValue("@name", txtname.Text);
            com.Parameters.AddWithValue("@image", data);
            con.Open();
            com.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Image Added");
        }


//Code  for  show image(binary format) in PictureBox  from table  …………………………

        private void btnLoadImage_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=KUSH-PC;Initial Catalog=test;Integrated Security=True");
            SqlCommand com = new SqlCommand("select Id,Name,UserImage from TableImage where Id=@id", con);
            com.Parameters.AddWithValue("@id", Convert.ToInt32(txtid.Text));
          
            con.Open();
            dr = com.ExecuteReader();
            if (dr.HasRows)
            {
                dr.Read();
                txtname.Text= ""+dr["Name"];
                objimage = dr["UserImage"];

                }

          //objimage = com.ExecuteScalar();
            byte[] data;
            data = (byte[])objimage;
            MemoryStream ms = new MemoryStream(data);
            pictureBox1.Image = Image.FromStream(ms);
            con.Close();
        }

//Code for  go to next page…………………………………………

        private void btnNextPage_Click(object sender, EventArgs e)
        {
            CopyFile cf = new CopyFile();
            cf.Show();
        }
      }
}


After retrieve  image  from table image is  stored  in binary format  and after retrieve  image show  in PictureBox Control in Windows Application in C# .NET






How  to  copy  image  from  another  location from  computer drive and paste  in other drive in computer in Windows Application 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.IO;

namespace FileHandlingExp
{
    public partial class CopyFile : Form
    {
        public CopyFile()
        {
            InitializeComponent();
        }

 
       // Code for next page
        private void btnNext_Click(object sender, EventArgs e)
        {
            StoreImageInDataBase si = new StoreImageInDataBase();
            si.Show();
        }

 // Code for  how  to select image  from Computer drive …………..

        private void btnSelectSource_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtsourceaddress.Text = openFileDialog1.FileName;
            }
        }

// Code for  copy  image  where is  paste in  Computer drive …………..

        private void btnLocateSource_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtdestinationaddress.Text = saveFileDialog1.FileName;
            }

          
        }
 // Code for  how  to save image  ……………………………………

        private void btnCopyPaste_Click(object sender, EventArgs e)
        {
            FileStream fssource = new FileStream(txtsourceaddress.Text, FileMode.Open);
            FileStream fsdest = new FileStream(txtdestinationaddress.Text, FileMode.Create);
            while (true)
            {
                int i;
                i = fssource.ReadByte();
                if (i == -1)
                    break;
                fsdest.WriteByte(Convert.ToByte(i));
            }
            fsdest.Close();
            MessageBox.Show("Image copy in Location");
          
        }
        
    }

}






0 comments:

Post a Comment