0

i have to display binary image in pictureBox (Winforms Application) but having the exception "Parameter is not valid". below is my code, i searched alot but could not found the desired solution.

        ClsCustomerTransaction ct = new ClsCustomerTransaction();
        byte[] photo_aray = (byte[])ct.GetPicture().Rows[0][0];
        MemoryStream ms = new MemoryStream();
        ms.Write(photo_aray, 0, photo_aray.Length);            
        pictureBox1.Image = Image.FromStream(ms);

The Image is saved in database by following code.

MemoryStream ms = new MemoryStream();
            byte[] PhotoByte = null;
            pictureBox1.Image.Save(ms, ImageFormat.Png);
            PhotoByte = ms.ToArray();
            ClsDressImages.Specification = txtSpecification.Text;
            ClsDressImages.Img = PhotoByte;
            if (ClsDressImages.SaveImage())
            {
                MessageBox.Show("Successfully saved");
                Reset();
            }

public class ClsDressImages
    {
        public static string table;
        public static string Specification { get; set; }
        public static byte[] Img { get; set; }

        public static bool SaveImage()
        {
            ClsDatabaseManager dbm = ClsDatabaseManager.InitializeDbManager();
            bool result = false;
            try
            {
                dbm.Open();
                result = dbm.ExecuteNonQuery("INSERT INTO " + table + " VALUES (N'" + Specification + "','" + Img + "')", CommandType.Text).ToBool();
                dbm.Dispose();
            }
            catch (Exception ex)
            {
                dbm.Dispose();
                throw ex;
            }
            return result;
        }
    }

Thanks in advance.

Loyal
  • 773
  • 2
  • 8
  • 20
  • are you sure the image in origin is not with problems? – NicoRiff Jan 17 '17 at 17:48
  • Try ms.Seek(SeekOrigin.Begin, 0) before calling Image.FromStream. – Gusman Jan 17 '17 at 17:48
  • @NicoRiff i added the code by which the image is being saved. so i think it would be correct. – Loyal Jan 17 '17 at 17:55
  • @Gusman doesn't work. – Loyal Jan 17 '17 at 17:55
  • Try to save the content of ms to a file and double check the content is right, besides that (unless you're executing the code in a secondary thread) the rest of the code is ok. – Gusman Jan 17 '17 at 17:58
  • @Gusman the rest of the code is just insert query and is my saving code not correct? – Loyal Jan 17 '17 at 18:01
  • No idea, I can't see the code at ClsDressImages nor at ClsCustomerTransaction. The easiest way to verify it is as I said to save the content of ms to a file, something like `using (var file = File.Create("output.png")) ms.CopyTo(file);` just after the ms.Seek and check if the content is right. – Gusman Jan 17 '17 at 18:04
  • @Gusman i checked the file, the content is not okay. i added the clsDressImages class code, just have a look at that. – Loyal Jan 17 '17 at 18:15
  • 2
    Ok, that's completely wrong, you can't concatenate a byte array to an string, `" VALUES (N'" + Specification + "','" + Img + "')"` is totally wrong, you need to use named parameters to store the binary data. – Gusman Jan 17 '17 at 18:19
  • i am not concatenating binary data to string, these are two fields in table one is "Specification" of type nvarchar and other is [Image] of type image. – Loyal Jan 17 '17 at 18:24

0 Answers0