-1

Im retrieving pictures from database, i want to display it to picturebox. getting exception "Not a valid parameter"

i already google it, and almost of it not solve my problem.

while (reader.Read())
{
      byte[] data = (Byte[])reader["P1"];
      MemoryStream mStream = new MemoryStream(); 
      mStream.Write(data, 0, Convert.ToInt32(data.Length));
      Bitmap bm = new Bitmap(mStream, false);
      mStream.Dispose();
      pictureBox1.Image = bm;
}

Its having an error on "Bitmap bm = new Bitmap(mStream, false);" is there something wrong with my data from the ms access database?

heres the printscreen of my code

Alexei Levenkov
  • 94,391
  • 12
  • 114
  • 159
Dodong
  • 29
  • 7
  • i can provide the ms access database for your reference if necessar – Dodong Dec 24 '18 at 05:25
  • Why don't you use `MemoryStream mStream = new MemoryStream(data);`? This way you use it it probably opens a write stream, that can not be used as input stream to read data for bitmap. Or at least use `Seek(0, SeekOrigin.Begin);` before reading from the stream. *(this is not necessary when you use `new MemoryStream(data)`)* – Julo Dec 24 '18 at 05:30
  • thanks @Julo i will try your suggestion now...then i will tell you if it works. – Dodong Dec 24 '18 at 05:38
  • Try the answer of TheGeneral. It has even better (correct) usage of `Bitmap` class. – Julo Dec 24 '18 at 05:40
  • it fails Julo , can i send the database? to check if theres somthing wrong in it? – Dodong Dec 24 '18 at 05:55
  • Sorry, I have no database at home installed and since today is holiday in my country I can not help you with this. Try save the file to disk and see what type of file it is, like TheGeneral suggested. It can be, that the format of the file is not supported by `PictureBox`, or there is no picture at all. – Julo Dec 24 '18 at 06:01
  • 1
    @Dodong Please stop offering to send the database to people: *"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it **in the question itself**."* Your question is of no value to future readers if the problem was not part of the question. And please read [ask] for the importance of the title, because *"Is there anybody out there to check whats wrong with my code..."* adds no value. – Richardissimo Dec 24 '18 at 07:18
  • [Duplicate](https://stackoverflow.com/questions/21555394/how-to-create-bitmap-from-byte-array) shows how to correctly create bitmap from bytes. If you bytes don't represent image in supported format - deal with it separately (and likely based on your comments problems with your DB migrations would not be on-topic here) – Alexei Levenkov Dec 24 '18 at 07:41

2 Answers2

1

If this doesn't work, then there is something wrong with your image

using (var ms = new MemoryStream(imageData))
{
   // lets be be good and dispose any previous images
   pictureBox1.Image?.Dispose();
   pictureBox1.Image = new Bitmap(ms);
}

If it fails, then try writing it to file and see if it opens in any image editor

TheGeneral
  • 69,477
  • 8
  • 65
  • 107
  • i will try your comment @TheGeneral i will comment back here if it works.. – Dodong Dec 24 '18 at 05:45
  • it fails, can i send you the database? to check if theres wrong in it? – Dodong Dec 24 '18 at 05:54
  • @Dodong in short no, who knows what viruses you have lurking in there. Did you save the bytes to file and check if its a valid image with an image editor – TheGeneral Dec 24 '18 at 05:56
  • this database was actually from a mysql database, i export it to ms access using navicat . is there a problem exporting mysql blob to ms access ? – Dodong Dec 24 '18 at 06:01
  • @Dodong i dont know, have you tried saving the image to a file, and testing it? this what you should be worried about... `File.WriteAllBytes` – TheGeneral Dec 24 '18 at 06:02
-1

Try below:

 if (reader.Read())
    {
           byte[] data = (Byte[])reader["P1"];
           using (MemoryStream mStream = new    MemoryStream(data))
              {
                   Bitmap bm = new Bitmap(mStream);

                   pictureBox1.Image = bm;
              }
     }
Sonal Borkar
  • 531
  • 1
  • 7
  • 12