1

I have a C# program in visual studio 2010 where I am accessing the data from my access database. I can get all of the information, except for the image. I have followed the steps here to embed the pictures in the access database.

Right-click the first field in the Image column of the table and click Insert Object.
Click Create from File, and then click Browse.
Browse to one or more Windows Bitmap (.bmp) or Device Independent Bitmap (.dib) images.      
You can find a set of BMP files, named Empid1.bmp through Empid9.bmp, at 
drive:\Program Files\Microsoft Office\OFFICE11\SAMPLES. Select the first image and click OK.

I used the location of my bitmap image though. I have a constructor that contains a bitmap attribute, but when it tries to go to the table to get all the information, I get the error: "Unable to cast object of System.Byte[] to type System.Drawing.Bitmap." Not sure why it is saying the image is stored as a system byte.

Found this thread. So I tried Memory streams, but same problem, can't convert system byte to system.io.memorystream.

Community
  • 1
  • 1

2 Answers2

0

Memory stream can be created from byte array and Image can be made from memory stream. The following code will compile:

byte[] bytes = new byte[0];  
MemoryStream ms = new MemoryStream(bytes);  
Image img = Image.FromStream(ms);
LINQ2Vodka
  • 2,996
  • 2
  • 27
  • 47
  • The thing is that I am using a constructor class to input the data from the database into the array. So I'm not sure how I would use the above in that situation. I changed my picture attribute to a byte instead of a memoryStream, but then I got the error that the specified cast in not valid. –  Nov 15 '13 at 03:20
0

The procedure you describe in your question for inserting bitmap images into an Access database will save the image imbedded in an OLE Object. If you want to use just the bitmap image in your C# program you need to remove the OLE "wrapper" from the binary data that is retrieved from Access.

For example, if I retrieve it from the Access database and try to convert it directly to a new Bitmap object...

private void Form1_Load(object sender, EventArgs e)
{
    using (var con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Public\Database1.accdb;"))
    {
        con.Open();
        using (var cmd = new OleDbCommand("SELECT LastName, FirstName, Photo FROM Clients WHERE ID=3", con))
        {
            OleDbDataReader rdr = cmd.ExecuteReader();
            rdr.Read();
            this.textBox1.Text = rdr["FirstName"].ToString();
            this.textBox2.Text = rdr["LastName"].ToString();
            byte[] photoBytes = (byte[])rdr["Photo"];
            var ms = new System.IO.MemoryStream(photoBytes);
            this.pictureBox1.Image = new System.Drawing.Bitmap(ms);
            ms.Close();
        }
        con.Close();
    }
}

...I get a "Parameter is not valid" error:

Parameter.png

However, if I remove the OLE "wrapper" using the GetImageBytesFromOLEField method of the OleImageUnwrap class in my other answer here...

var ms = new System.IO.MemoryStream(OleImageUnwrap.GetImageBytesFromOLEField(photoBytes));

...then it works:

Hank.png

Community
  • 1
  • 1
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • For the picturebox.Image, what namespaces and references do I need? It says it cannot find a definition for Image. I thought it was system.windows.controls.listbox, but I cannot get to the controls part with a namespace space or reference of system.windows.controls. –  Nov 21 '13 at 22:13
  • @user2781018 `PictureBox.Image` is part of `System.Windows.Forms` (ref: [here](http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.image%28v=vs.110%29.aspx)). – Gord Thompson Nov 21 '13 at 23:25
  • I'm an idiot. I wasn't using a picture box. Thanks. It's working now. How long did it take you to write that method? I know it strips the headers in such but there isn't a chance I could have written that. Thanks again. –  Nov 22 '13 at 03:02
  • @DarthSucuk - https://stackoverflow.com/a/19709053/2144390 – Gord Thompson Sep 17 '20 at 16:19