none
Parameters not valid(retrieving image from database) RRS feed

  • Question

  • Hi i searched many places for the answer for this but it seems like all the places that i find does not give me the solution to this problem...

     con.Open();
                string SqlQuery = "Select Background from Backgrounds where UserID = '" + 1 + "'";
                cmd = new SqlCommand(SqlQuery, con);
                SqlDataReader dr = cmd.ExecuteReader();
                dr.Read();
                if(dr.HasRows)
                {
                    byte[] images = (byte[])dr[0];

                    if (images == null)
                    {
                        pictureBox1.Image = null;

                    }
                    else
                    {
                        MemoryStream ms = new MemoryStream(images);
                      pictureBox1.Image = Bitmap.FromStream(ms); <-error appears here

    }

    }

    Please advice on how to solve this problem thank you...

    Thursday, December 7, 2017 1:06 AM

All replies

  • Make sure that the database contains correct data. The images array should not be empty.

    In order to check that the image is valid, use File.WriteAllBytes to write the images array to a file, then try opening the file using some graphical editor, like Paint.

    Give details about the data table and about the procedures used to insert the images.

    Thursday, December 7, 2017 8:15 AM
  • If the code would be running on WinXP/Win2003, it could also be caused by trying to open image with encoding parameters not supported by GDI+ 1.0. (In the case that I've encountered last week, it's about animated GIF created on Win7 machine constantly crashing Image.FromStream() on WinXP. It's because EncoderValue.FrameDimensionTime is not supported there. It turns out that GDI+ 1.0 can only render animated GIF that uses "delay" in image frame, but cannot render animated GIF that marked image frame with this parameter and use propertyitem to control instead.)

    To test for this case, just try to save that image as file and open it with the default image viewer in WinXP. If it says it cannot load the image then it is the case.

    Thursday, December 7, 2017 9:23 AM
    Answerer
  • No its running on the windows 10...
    Friday, December 8, 2017 8:28 AM
  • Hi Bob3253,

    Please do the step in the video. It shows how to save and retrieve image from Database using C# and SQL Server in Win10.

    https://www.youtube.com/watch?v=W_cOlBBlFGM 

    Best Regards,

    Wendy

    Note: This response contains a reference to a third-party World Wide Web site. Microsoft is providing this information as a convenience to you. 
    Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.
    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet. 

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 12, 2017 8:15 AM
    Moderator
  • Hi Bob3253,

    As Viorel said, please check if the image field is a image type. like this:

    and change your code like this:

    MemoryStream ms = new MemoryStream(images);
    pictureBox1.Image = Image.FromStream(ms);

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 12, 2017 8:20 AM
    Moderator