Hi
I have an access database that has a column in a table that has bmp images stored as ole object type. I have a c# windows form with a picturebox control. I need to be able to retrieve an image from the database but i do not know how. The event is triggered by a button. The database contains film titles and their cover images. The data is loaded into a dataset and then i use sql statements in the table adapters to return the desired row from the table, Here is the code so far,
string selGenre = cbGenre.SelectedItem.ToString(); if (selGenre == "All")
{
int iMin = 1; int iMax = filmsDataSet.Films.Rows.Count; int iRand = RandomFilm(iMin, iMax);txtFilm.Text = filmsTableAdapter.FillByFilm(iRand).ToString();
byte[] byImage = new byte[0];byImage = (
byte[])(filmsTableAdapter.ImageFromID(iRand)); MemoryStream stmImage = new MemoryStream(byImage);pictureBox1.Image =
Image.FromStream(stmImage);}
But i get an exception withe the line - byImage = (byte[])(filmsTableAdapter.ImageFromID(iRand));
Any help would be much appreciated.
Thanks
theLizardPosted Jan 8, 2010, 5:10 AM
I don't know much about access and don't want to know but by the looks of things you are casting this filmsTableAdapter.ImageFromID(iRand) to a byte array with zero elements ( = new byte[0]) and I think this is the reason for the exception.
If the image is stored as an ole object, are you sure that the image itself is actually in the database and not invoking ole automation when called for!
But I could be wrong.
Lalit MPosted Jan 8, 2010, 2:34 AM
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 =
}