Click here to Skip to main content
15,893,923 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello sir,
I am a trainee of c#. recently I got a problem to save a image (jpg format) from a picturebox to database. I am using sql database. and visual studio 2008. i can show any image to picture box in my window application. but could not save that image in database.

plz if u could solve my problem.

thank you,
Md. Shafiqul Azam
Dhaka,
Bangladesh
Posted

Hi we do not save image in database , we save the only image path database, so you should be save your image in a folder, and its path in database, and for more information you can visit this url...Store or Save images in SQL Server[^]
 
Share this answer
 
Declare this variable:
----------------------

Byte[] b = null;
FileStream Fs = default(FileStream);


click the pictureBox1 then write this coding:
---------------------------------------------

C#
private void PictureBox1_Click(object sender, EventArgs e)
       {
           try
           {
               OpenFileDialog dialog = new OpenFileDialog();

               dialog.Filter = "JPEG(*.jpeg)|*.jpeg|GIF(*.gif)|*.gif|PNG(*.Png)|*.png|AllFiles|*.*";
               if (dialog.ShowDialog() == DialogResult.OK)
               {
                   Fs = File.Open(dialog.FileName, FileMode.OpenOrCreate);
                   b = new Byte[Fs.Length];
                   Fs.Read(b, 0, b.Length);
                   Fs.Close();
                   IMG.Image = Image.FromFile(dialog.FileName);
               }
               else
               {
                   MessageBox.Show("Select Image", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }
       }



Click save Button:
-----------

SqlCmd.Parameters.AddWithValue("@img", b);
 
Share this answer
 

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