Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My original code to fetch image is :

C#
private void loadImage()
        {
            cn = new SqlConnection(con);
            da = new SqlDataAdapter("Select empimage from newemployeerecord where employeeid='2015160000'", cn);
            ds.Tables.Clear();
            da.Fill(ds, "Image");
            if (ds.Tables["Image"].Rows.Count > 0)
            {
                pbUploadPhoto.Image = null;
                if (ds.Tables[0].Rows[0][0] != DBNull.Value)
                {
                    photo_aray = (byte[])ds.Tables[0].Rows[0][0];
                    MemoryStream ms = new MemoryStream(photo_aray);
                    pbUploadPhoto.Image = new Bitmap(ms); // Exception Part...ArgumentException Was Unhandled  (parameter is not valid)
                    pbUploadPhoto.Refresh();
                }
            }
        }


When i run this code i am getting error i.e.
ArgumentException was unhandled
parameter is not valid.

For this i googled so many times but not getting right solution as i am don't want to use FileStream Class.

pls help as i am new with this error...
Posted
Updated 16-Nov-15 7:14am
v6
Comments
PIEBALDconsult 15-Nov-15 14:06pm    
Which line is throwing the Exception?
Anjanee Kumar Singh 15-Nov-15 14:19pm    
pbUploadPhoto.Image = new Bitmap(ms);
PIEBALDconsult 15-Nov-15 14:26pm    
Use "Improve question" to add that to the question.
Anjanee Kumar Singh 15-Nov-15 15:02pm    
Question Improved as you suggested.
Afzaal Ahmad Zeeshan 15-Nov-15 15:22pm    
Is the MemoryStream actually a graphics or bitmap object?

Well, to save the data in a file, you can use the FileStream class[^].
C#
using (FileStream fs = new FileStream(@"C:\Test.jpg"))
{
    byte[] fileData = (byte[])ds.Tables[0].Rows[0][0];
    fs.Write(fileData, 0, fileData.Length);
    fs.Flush();
}

To view the file as an image you can use any of the pre-installed applications on Windows, such as Paintor the default image viewer.

To open the file and view the binary data, you can use Visual Studio, UltraEdit or any other binary editor.

Remember, this is not a solution. Only help to debug your problem.

As a pointer for how to read data from the database, you can look into the DataReader[^] class instead of using a DataAdapter which is kind of overkill when you want to read only one row with one column.
See also Retrieving Data Using a DataReader[^]
 
Share this answer
 
Comments
Anjanee Kumar Singh 16-Nov-15 13:20pm    
Thanks To All Of You Guys For Helping Me again.............
In DB
create table test
(
Photoid varchar(10),
Photo Image
)

To Select A Image and preview it in picturebox
C#
private void btnImgBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = new Bitmap(ofd.FileName);
                textBox1.Text = ofd.FileName;
            }
        }


To Save In DB

private void btnSave_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("Data Source=Server;Initial Catalog=Test;Integrated Security=True");
            try
            {
                byte[] image = null;
                FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);
                image = br.ReadBytes((int)fs.Length);
                cn.Open();
                string query = "insert into images (photoid,photo)values('" + textBox2.Text + "',@img)";
                SqlCommand objcmd = new SqlCommand(query, cn);
                objcmd.Parameters.Add(new SqlParameter("@img", image));
                int rowaffected = objcmd.ExecuteNonQuery();

                MessageBox.Show("Inserted Successfully");

                MessageBox.Show(rowaffected + "row'(s)" + "affected");
                
                cn.Close();

            }

            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);

            }
        }



To Show Image Back In PictureBox

private void btnShowImage_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("Data Source=Server;Initial Catalog=Test;Integrated Security=True");
            cn.Open();
            SqlCommand cmd = new SqlCommand("select photo from images where photoid="+textBox2.Text,cn);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                byte[] img = (byte[])dr[0];
                MemoryStream ms = new MemoryStream(img);
                pictureBox1.Image = Image.FromStream(ms);
            }
            cn.Close();
        }     
 
Share this answer
 
Comments
George Jonsson 16-Nov-15 18:38pm    
If you are going to answer your own question, at least explain what was wrong.
Just dumping code is not so helpful for others with the same problem.
Anjanee Kumar Singh 24-Nov-15 9:52am    
I am sorry @George sir..

I was not saving the image in a right way. Above solution i got as you suggested me... i followed your way.... that's it. sorry i forgot to accept your solution as Answer...


Thank You

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