0

We have a legacy system in MSAccess which has a "OLE Object" Column to store images. Now, we are trying to create a small executable to convert the "OLE object" back to image file using C#. We came to know the users takes screenshot and paste dierctly to the MSAccess column.

But while retrieving data from access it returns the bytes but throws 'Parameter is not valid.' exception while doing Image.FromStream(ms).

Here is my sample code.

//Connect to MSAccess database
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
try
{
  DataTable dt = new DataTable();
  using (OleDbCommand cmd = new OleDbCommand(commandText, conn))
  {
    using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
    {
      da.Fill(dt);
      if (dt != null && dt.Rows.Count > 0)
      {
                                foreach (DataRow dr in dt.Rows)
                                {
                                    Byte[] imageBytes = dr[0] as Byte[];                                    
                                    MemoryStream ms = new MemoryStream(imageBytes);
                                    Image img = Image.FromStream(ms);
                                    img.Save(destinationPath + "Image1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);                                    
                                }
                            }
                        }
                    }
                }
       finally
       {
         conn.Close();
       }
} 

Any help is highly appreciated.

Deb
  • 23
  • 1
  • 5
  • possible duplicate of [Convert Access image OLE Object into raw image byte array in C#](http://stackoverflow.com/questions/19688641/convert-access-image-ole-object-into-raw-image-byte-array-in-c-sharp) – Gord Thompson Dec 24 '14 at 13:26
  • The resolution suggested in the other links did not work. The end users took screenshot and pasted(Ctrl+V) in OLE object column. So, it does not fall in any category like JPG, PNG etc. – Deb Jan 06 '15 at 00:54

0 Answers0