Skip to content

Access Database

Duane Spainhour

Asked Jul 28, 2008, 7:39 PM EDT3 answers
Share
LinkedInX
Hi.. I'm working with databases, Access databases to be exact, and I have a database in which there is an OLE Object type to store pictures. I have no problem retrieving these pictures and showing them in a picturebox, and actually I have no problem saving them to the database. The problem arises when I try to RETRIEVE the pictures that I'VE saved to the database through the application I've written. If I put an image in via access, I can retrieve no problem. If I put an image in via my windows form, and later try to retrieve it and throw it back into a picture box, I get the following error:

System.ArgumentException: Parameter is not valid.
at System.Drawing.Bitmap..ctor(Stream stream)



I suppose, since the error ONLY arrives when I try to bring in one of the pictures saved through the form that it is the method in which its being saved, but I'll post the code for both saving and retrieving.

Saving:
Byte[] byPicture;
MemoryStream ms = new MemoryStream();
picPicture.Image.Save(ms, picPicture.Image.RawFormat);
byPicture = ms.ToArray();
..........
DataRow newRow = thisConnection.adoDataSet.Tables["Categories"].NewRow();
newRow["Picture"] = byPicture;



Retrieving:
Byte[] byPicture;
MemoryStream ms = new MemoryStream();
byPicture = (Byte[])thisConnection.adoDataSet.Tables["Categories"].Rows[rowPos]["Picture"];
ms.Write(byPicture, 78, byPicture.Length - 78);
try
{

     Bitmap bm = new Bitmap(ms);
     picPicture.Image = bm;
}
catch (ArgumentException ae)
{
      string message = ae.ToString();
      MessageBox.Show(message);
}

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • +1

    Hmm.. I read through it, and I'm not trying to do this via a website. I'm trying to do this via windows forms application, and I think it has something to do with pulling it out of the PictureBox. I've tried pulling from picPicture.Image.RawFormat, and as I said, it definately saves it but I can't turn around and open it back up. Maybe there's something other than .RawFormat? If so, I can't find it - everything else (just trying picPicture.Image, for example) says its neither explicitly or implicitly convertable.

     

    Any other suggestions?

    +1
  • +1
    I figured it out. The 78bit header is only added when an image is put directly in the database via MS Access. As I didn't save it with a header in the running of my program, the Byte[] was off.

    Also - can anyone tell me how to determine a buffer/header size on a file?? Thanks

    - Duane
    +1
  • 1 short reply
    • Bechir Bejaoui
      +1
      take a look on this
      +1