Memorystream parameter not valid in c#?

Asked By 30 points N/A Posted on -
qa-featured

Hi guys,

I'm developing a system for our Human Resource Management. The system includes displaying of employee's profile with picture. But I got some trouble on the employee's picture. I have successfully stored it on mysql database, however, when I tried to retrieve it, an error occurred. MemoryStream parameter was not valid. Can anyone help me on this?Here is my code below:

 

            string query = "select pic, picsize from employee";
            MySqlConnection con = new MySqlConnection(db.getConnection());
            MySqlCommand cmd = new MySqlCommand(query, con);
            try
            {
                con.Open();
                data = cmd.ExecuteReader();
                if (data.Read())
                {

                    int filesize = data.GetInt32(data.GetOrdinal("picsize"));
                    byte[] img = new byte[filesize];
                    data.GetBytes(data.GetOrdinal("pic"), 0, img, 0, filesize);
                    MemoryStream ms = new MemoryStream(img); //error goes here
                    pic.Image = Image.FromStream(ms);

                }
                con.Close();
            }
            catch
            {
                MessageBox.Show("Unable to load ID picture!", "Picture Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            finally
            {
                con.Close();
            }

SHARE
Best Answer by Vaughn Granier
Answered By 0 points N/A #86509

Memorystream parameter not valid in c#?

qa-featured

The common reason for the error message "Parameter is not valid" is because the data in the byte array is not a valid “image” format.  When an empty byte array or an invalid file format has been used, an ArgumentException will be thrown by the Image.FromStream method.  First of all, you should check and make sure that you were able to save a valid image into the database field before trying to retrieve it.  If you are sure that you have a valid image, you can try this code and see if it works:

            ImageConverter imageConverter = new System.Drawing.ImageConverter();

            Image image = imageConverter.ConvertFrom(byteArray) as Image;

Best Answer
Best Answer
Answered By 0 points N/A #196002

Memorystream parameter not valid in c#?

qa-featured

Hi,

Despite this and some other gdi + exceptions are very misleading. The problem is the parameter being sent to Bitmap constructor is not valid . The  code is here,
Using (System. IO. FileStream fs = new System. IO. FileStream (inputImage, System. IO. FileMode. Open, System. IO. FileAccess. ReadWrite))
{ Try { using (Bitmap bitmap = (Bitmap) Image. FromStream (fs, true, false)) { try { bitmap. Save (OutputImage + ". bmp", System. Drawing. Imaging. ImageFormat. Bmp); GC. Collect (); } catch (Exception ex) { throw ex; } } } catch (ArgumentException ex) { throw new Exception ("The file received from the Map Server is not a valid JPEG image", ex); } }

Thanks

Login/Register to Answer

Related Questions