Click here to Skip to main content
15,909,445 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everybody,

I want small help from you guys. I have some images. I need the images to store in sql server database with the format of binary and the same image has to convert back from binary while retrieving from database and placed in picture box in windows application.

Can you please help me to do this.

Thanks& Regards,
Venkata Sandeep P
Posted
Updated 26-Mar-11 20:23pm
v2
Comments
Sandeep Mewara 27-Mar-11 2:23am    
Make effort now, if you face specific issue then post back.

Yes you can do this easily. Set the datatype of the field
"Image" in which you want to store the Image. Now you have to convert the image in byte array(byte[]). Now by Query or Stored procedure insert data in the table. To retrieve the Image use reverse process.

To convert image or any other object to byte array you can use the function written below.....
public byte[] ObjectToByteArray(Object obj)
        {
            if (obj == null)
                return null;
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, obj);
            return ms.ToArray();
        }

Convert a byte array to an Object
public Object ByteArrayToObject(byte[] arrBytes)
      {
           MemoryStream memStream = new MemoryStream();
           BinaryFormatter binForm = new BinaryFormatter();
           memStream.Write(arrBytes, 0, arrBytes.Length);
           memStream.Seek(0, SeekOrigin.Begin);
           Object obj = (Object)binForm.Deserialize(memStream);
           return obj;
       }
 
Share this answer
 
Comments
sandeep.paruchuru 27-Mar-11 10:33am    
how to store that image into the database please i want complete explanation
Here, go through this article, it has more than you seek: C# Photo Album Viewer[^]
 
Share this answer
 
you shuold do this:

using system.io;
using system.data.sqlclient; 



public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection();
        string _path;
        public Form1()
        {
            InitializeComponent();
        }

        //convert Image to binary and save in DB
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {

                    _path = openFileDialog1.FileName;

                   
                    InsertInSQL(_path);
                    
                
            }
          
        }

        private void InsertInSQL(string _path)
        {
            con.ConnectionString = Pic.Properties.Settings.Default.ConnectionS;
        string strQ = "insert into dbo.PicTBL(Pic)values(@p)";
        SqlCommand command = new SqlCommand(strQ,con);
        command.Parameters.AddWithValue("@p",ImageToBinary(_path));
        con.Open();
        command.ExecuteNonQuery();
        con.Close();
        }      

        public static byte[] ImageToBinary(string _path)
        {
            FileStream fS = new FileStream(_path, FileMode.Open, FileAccess.Read);
                   byte[] b = new byte[fS.Length];
                   fS.Read(b, 0, (int)fS.Length);
                   fS.Close();
                   return b;
        }

                    
 



         //Convert Binary to imge and save in a folder



        private void button1_Click_1(object sender, EventArgs e)
        {
                     

            DataTable dt = Rimage();

            foreach (DataRow row in dt.Rows)
            {
                byte[] b = (byte[])row["Pic"];
                Image img = BinaryToImage(b);
                img.Save("D:\\NewFolder\\" + row["ID"].ToString() + ".jpg");
            }

            
       }

         private Image BinaryToImage(byte[] b)
          {
            if (b == null) 
                return null;
         
    
           MemoryStream memStream = new MemoryStream();
           memStream.Write(b, 0, b.Length);

           return Image.FromStream(memStream);
 	
           }

               private DataTable Rimage()
                 {
                 con.ConnectionString = Pic.Properties.Settings.Default.ConnectionS;
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "select * from dbo.PicTBL";
                 cmd.Connection = con;
                 SqlDataAdapter adp = new SqlDataAdapter(cmd);
                 DataTable dt = new DataTable();
                 con.Open();
                 adp.Fill(dt);
              
                 return dt;

        }
       

       
    }
 
Share this answer
 
v2
Comments
Sandeep Mewara 8-Jul-12 2:55am    
This is more than 1.5yrs old question, do you think OP will be still waiting for an answer?
Mina Mansouri 9-Jul-12 1:43am    
I did not mention to its time :D
Mina Mansouri 9-Jul-12 1:43am    
and so what do you do here?????!!!!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900