0

I saved my image to MySql Database using wamp server localhost but when i am trying to retrieve image from MySql and convert the image from bytes to image it gives the error parameter is not valid, i used many techniques and many solution but in vain, i user image converter and memory stream but didn't wok for me i am pasting my code please help me its very important.

byte[] img;

MySqlCommand cmd_image = new MySqlCommand("Select image from logindetails where Username = '" + txtUsername.Text + "'", con);
MySqlDataReader Image_Reader = cmd_image.ExecuteReader();
            while (Image_Reader.Read())
            {
                img = (byte[])Image_Reader["Image"];
            }

            System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
            Image image = (Image)converter.ConvertFrom(img);
            pictureBox1.Image = image;
Usman Liaqat
  • 1,771
  • 2
  • 7
  • 6
  • 1
    Possible duplicate of [Byte Array to Image Conversion](https://stackoverflow.com/questions/9173904/byte-array-to-image-conversion) – Johnny Mar 02 '18 at 14:13
  • This is crazy-vulnerable to sql injection attacks. It's practically begging to get hacked. – Joel Coehoorn Mar 02 '18 at 14:38
  • The only thing that really matters to answer this question is, In what format is the image _saved_ in the database? – Nyerguds Mar 03 '18 at 19:06

1 Answers1

0

ImageConverter is used to convert images, not create them. Use the code below:

    Bitmap bitmap;
    using (MemoryStream memoryStream = new MemoryStream(img))
    {
        using (Image imageFromStream = Image.FromStream(memoryStream))
        {
            bitmap = new Bitmap(imageFromStream);
        }
    }
    pictureBox1.Image = bitmap;

Note that if your byte array does not hold an image you will still get the same error.

Vqf5mG96cSTT
  • 2,561
  • 3
  • 22
  • 41
  • i tried to your code but it is giving the same error – Usman Liaqat Mar 03 '18 at 15:24
  • and array img has 13 bytes – Usman Liaqat Mar 03 '18 at 15:27
  • @UsmanLiaqat What is the data type of the column image in the table logindetails? – Vqf5mG96cSTT Mar 03 '18 at 16:25
  • 1
    [Don't create an image from a memory stream you dispose. It's against specifications.](https://stackoverflow.com/a/48579791/395685) There are workarounds for this. – Nyerguds Mar 03 '18 at 19:05
  • 13 bytes seems rather little to create an image from. Are you sure that's actual image data? It barely seems enough to be a path to a file, or even just a filename... – Nyerguds Mar 03 '18 at 19:07
  • @UsmanLiaqat 13 bytes is indeed unbelievably small. A small logo is easily 2000 bytes. This could be wrong data which is why you are still receiving the same error. – Vqf5mG96cSTT Mar 03 '18 at 19:49
  • 1
    @Nyerguds You are correct, I just tested it. I thought it was only for storing images to files. But indeed after the stream is disposed the image can no longer be viewed. I have adjusted my answer. – Vqf5mG96cSTT Mar 03 '18 at 20:00
  • You should also dispose the image you made from the stream, though. – Nyerguds Mar 21 '18 at 23:27