I want to retrieve a image stored in MS Access database as an OLE object.
while using a OledbDatareader and getValue method, its returning me a byte array.

how can i use this array to display my picture as it is in a picturebox on a form.

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\db1.mdb");
        conn.Open();
        string query = "select * from Images_Table";
        OleDbCommand comnd = new OleDbCommand(query, conn);
        OleDbDataReader rdr = comnd.ExecuteReader(CommandBehavior.SequentialAccess);
        while (rdr.Read())
        {

            label1.Text = (string)rdr.GetValue(0);
            pictureBox1.Image=rdr.GetValue(1); //ERROR !!!
//rdr returns a byte array.
//How can i set the image in picturebox1.


        }

        rdr.Dispose();
        conn.Close();
    }

Please help....
Thank You.

Recommended Answers

All 3 Replies

byty is the array of byte returned from your DB

MemoryStream ms=new MemoryStream(byty);
			pictureBox1.Image=Image.FromStream(ms);

Hi
I wrote this code to retrieve the image from MS Access DB.

        byte[] ImageByte = null;
        MemoryStream MemStream = null;
        PictureBox PicBx = new PictureBox();
        object OB;

        string WorkingDirectory = Application.StartupPath + "";
        connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + WorkingDirectory + "DBFile.mdb; Persist Security Info=True";
        cnction = new OleDbConnection(connString);
        cnction.Open();
        int ImageID = 6;
        sqlCommand = "SELECT ImageObject FROM ImagesTable WHERE ImageID = " + ImageID + "";
        comm = new OleDbCommand(sqlCommand, cnction);
        rdr = comm.ExecuteReader();
        while (rdr.Read())
        {
            OB = rdr.GetValue(0);
        }

        MemStream = new MemoryStream(ImageByte);
        PicBx.Image = Image.FromStream(MemStream);
}

but the variable OB gets an array of just 26 elements that dont contain the image.
Pleaze HELP.

Let's modify your code to this

byte[] ImageByte = null;
MemoryStream MemStream = null;
PictureBox PicBx = new PictureBox();
object OB;

string WorkingDirectory = Application.StartupPath + "\\";
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + WorkingDirectory + "DBFile.mdb; Persist Security Info=True";
cnction = new OleDbConnection(connString);
cnction.Open();
int ImageID = 6;
sqlCommand = "SELECT ImageObject FROM ImagesTable WHERE ImageID = " + ImageID + "";
comm = new OleDbCommand(sqlCommand, cnction);
ImageByte = comm.ExecuteScalar();
MemStream = new MemoryStream(ImageByte);
PicBx.Image = Image.FromStream(MemStream);
}
Works?
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.