0

I want to read image from database and show in picture box. I tried sample example it runs perfectly.But when I use that code in my project then it not works shows that error "Parameter is not valid".please let me know if anyone have solution.thanks in advance.

 string ID = dataGridView1.CurrentRow.Cells["ID"].Value.ToString();
        cmd.CommandText = "SELECT NEW_DRAWING FROM PO_ITEM WHERE ID='"+ID+"'";
        SqlDataReader reader = cmd.ExecuteReader();
        reader.Read();

            byte[] img = ((byte[])reader[0]);
            if (img == null)
                pictureBox1.Image = null;
            else
            {
                  MemoryStream ms = new MemoryStream(img);
                pictureBox1.Image = Image.FromStream(ms,true);
            }
Komal D.
  • 1
  • 4
  • what is the value of ID ? is it non empty ? – user9405863 Apr 07 '18 at 10:04
  • Store one image on disk _and_ in the database, load both and compare the `byte[]` you receive. If they're equal, you know the problem is the image data itself; if they are different, there's a problem with either how you store or retrieve the data. – C.Evenhuis Apr 07 '18 at 10:05
  • it represents value of row id..no its not empty – Komal D. Apr 07 '18 at 10:06
  • 1
    Have you tried resetting the position of the `MemoryStream` after writing to it? Just add `ms.Position = 0;` after calling `Write`. As an aside, now would be a good time to use parameterized SQL rather than building SQL dynamically. – Jon Skeet Apr 07 '18 at 10:07
  • Might help you -- https://stackoverflow.com/questions/17667818/error-parameter-is-not-valid-while-converting-bytes-into-image –  Apr 07 '18 at 10:10
  • Hi C.Evenhuis,I tested, both shows different bytes for same image while reading when I insert image byte values is 58053 and while reading it read 9 – Komal D. Apr 07 '18 at 10:21
  • hi daisy,but its not working – Komal D. Apr 07 '18 at 10:22
  • Possible duplicate of [What are good ways to prevent SQL injection?](https://stackoverflow.com/questions/14376473/what-are-good-ways-to-prevent-sql-injection) – mjwills Apr 07 '18 at 10:28
  • Which line throws the exception? – mjwills Apr 07 '18 at 10:29
  • pictureBox1.Image = Image.FromStream(ms,true); this one – Komal D. Apr 07 '18 at 10:39
  • So, you just changed the code in your post. Does the same exception get thrown in the same place? If so, how did you write the data to the database in the first place? – jmcilhinney Apr 07 '18 at 10:41
  • By the way, why not call `ExecuteScalar` if you only want to retrieve one value? That's what it's for. – jmcilhinney Apr 07 '18 at 10:42
  • Also, why call `Read` and then test `HasRows`? There's no point to doing both. `Read` is going to return the same value as `HasRows` anyway. You ought to learn what this code you copied and pasted off the web actually does. Then you might understand why it works or doesn't. – jmcilhinney Apr 07 '18 at 10:43
  • now I have changed it but still getting same problem – Komal D. Apr 07 '18 at 10:44
  • changed but not working – Komal D. Apr 07 '18 at 10:59
  • This might help you : https://stackoverflow.com/questions/10454595/loading-picturebox-image-from-database?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Mirko Acimovic Apr 07 '18 at 12:24
  • Solved...there is wrong when I was inserting image in table..thanks to all – Komal D. Apr 09 '18 at 08:24

1 Answers1

-2

Image.FromStream() usually takes single parameter, that is MemoryStream.

 MemoryStream ms = new MemoryStream(img);
 pictureBox1.Image = Image.FromStream(ms);
Mirko Acimovic
  • 508
  • 2
  • 9
  • There are overloads that have bool parameters. https://msdn.microsoft.com/en-us/library/system.drawing.image.fromstream(v=vs.110).aspx – Loofer Apr 07 '18 at 12:30