none
I am facing a Problem with reading images from database RRS feed

  • Question

  • Hi everybody..

    any help will be most appreciated, I am facing problem with reading images from database. I am pasting my code... 


                    string connect = "datasource = localhost; port = 3306; username = root; password = ;"; 
                    MySqlConnection conn = new MySqlConnection(connect); // creating connecting string
                    MySqlCommand sda = new MySqlCommand(@"select * from management.add_products ", conn); //creating query
                    MySqlDataReader reader; 
                    try
                    {
                        conn.Open(); // Opening Connection
                        reader = sda.ExecuteReader(); // Executing my Query..
                        while (reader.Read())
                        {
                            byte[] imgg = (byte[])(reader["Picture"]);
                            if (imgg == null)
                                pc1.Image = null;
                            else
                            {
                                MemoryStream mstream = new MemoryStream(imgg);
                                pc1.Image = System.Drawing.Image.FromStream(mstream);
                            }
                    }

    It says Parameter not Valid... i am reading all the images from database


    Thursday, January 8, 2015 11:16 AM

Answers

  • I agree with Viorel. You are getting the error because the format of the data is incorrect probably because the data was modify. It may not be the reading of the database the is incorrect, but the application that wrote the data into the database. You need to compare the imgg array data with the data before it was written to the database to see if the data matches.  I usually start by comparing the number of bytes which is easier to check then compare the actual to isolate which function is changing the byte count.

    An image is binary data.  The standard VS methods for reading and writing data (usually stream classes) default to ASCII encoding which will corrupt binary data.  The solution usually is to use UTF8 encoding instead of the default ascii encoding.  Ascii encoding with stream often aligns the data and adds extra null bytes to the end of the data which can produce these type errors.


    jdweng

    Thursday, January 8, 2015 6:21 PM
  • I can just tell you that this is unlikely to be a SQL problem, because if that is the case, the exception should be thrown at "imgg = (byte[])(reader["Picture"])" line, not "Image.FromStream(mstream)" line. 

    I think your image could be a JPG image, and it's using a encoder that your computer is not supported (like, AFAIK, .NET runtime cannot handle CMYK Jpegs), so a "parameter error" is thrown.

    Why not just save the data to disk a try to open it with image editor and see what does it say?


    Friday, January 9, 2015 6:35 AM
    Answerer

All replies

  • It isn't clear from you posting which line is giving the failure. Make sure the field name in the database it "Picture" with a capital P. The name is case sensitive. If this isn't the issue, then step through the code to find which line is giving the error.

    jdweng

    Thursday, January 8, 2015 1:36 PM
  • the spelling of Picture is correct
    this line of code is giving error..

    pc1.Image = System.Drawing.Image.FromStream(mstream);

    here pc1 is PictureBox...

    Thursday, January 8, 2015 2:56 PM
  • Maybe the data are incorrect, i.e. the bytes returned by reader[“Picture”] do not represent a valid and complete image. Check this aspect and give some details about the “picture” column. How is it declared?


    • Edited by Viorel_MVP Thursday, January 8, 2015 5:32 PM
    Thursday, January 8, 2015 5:32 PM
  • I agree with Viorel. You are getting the error because the format of the data is incorrect probably because the data was modify. It may not be the reading of the database the is incorrect, but the application that wrote the data into the database. You need to compare the imgg array data with the data before it was written to the database to see if the data matches.  I usually start by comparing the number of bytes which is easier to check then compare the actual to isolate which function is changing the byte count.

    An image is binary data.  The standard VS methods for reading and writing data (usually stream classes) default to ASCII encoding which will corrupt binary data.  The solution usually is to use UTF8 encoding instead of the default ascii encoding.  Ascii encoding with stream often aligns the data and adds extra null bytes to the end of the data which can produce these type errors.


    jdweng

    Thursday, January 8, 2015 6:21 PM
  • the Picture Column is declared as a Blob data type, it reads the first image and then says that Parameter not valid..
    Friday, January 9, 2015 4:22 AM
  • I can just tell you that this is unlikely to be a SQL problem, because if that is the case, the exception should be thrown at "imgg = (byte[])(reader["Picture"])" line, not "Image.FromStream(mstream)" line. 

    I think your image could be a JPG image, and it's using a encoder that your computer is not supported (like, AFAIK, .NET runtime cannot handle CMYK Jpegs), so a "parameter error" is thrown.

    Why not just save the data to disk a try to open it with image editor and see what does it say?


    Friday, January 9, 2015 6:35 AM
    Answerer