diplaying bitmap from access database
I've seen this question posted a few times but no good results. I'm simply loading a dataset from access that happens to have a bitmap stored in a field. I have a listview with a list of CD's by Album/Artist, when I click on one of the CD's I want detailed info to populate below, including a pic box with the stored bitmap. Everything works fine except the pic box. Here's the code I have:
Dim
bytPic()AsByteobjDataSet =
New DataSetobjData.SQL =
"usp_SelectRecording"objData.InitializeCommand()
objData.AddParameter(
"@RecordingID", OleDb.OleDbType.Integer, 4, lvwRecordings.SelectedItems.Item(0).Tag)objData.FillDataSet(objDataSet,
"Recording")bytPic =CType(objDataSet.Tables("Recording").Rows(0).Item("AlbumCover"),Byte())Dim strPicAsNew IO.MemoryStream(bytPic.Length)strPic.Write(bytPic, 0, bytPic.Length)
picCover.Image =
New Bitmap(Image.FromStream(strPic))I get invalid parameter with this. Anyone have a solid method for doing this. thanks
where do you get the error message from? which part of the code?
I did write something like this for someone else on obtaining the images from a database....
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=710414&SiteID=1
this was using SQL however but shouldnt require many changes for it to be converted to the OleDb approach if your solution (it appears to) requires it
does that help you or give you any pointers?
I will further analyze and see where the problem may lie in your code
invalid parameter error happens on this line:
picCover.Image = New Bitmap(Image.FromStream(strPic))
your original post is where i got the idea for the code i wrote.
thanks
ok, I've managed to get it working for the OleDb.
Writing:
| | Dim theFS As New FileStream(Application.StartupPath & "\testImage.JPG", FileMode.Open) Dim theBytes(theFS.Length) As BytetheFS.Read(theBytes, 0, theBytes.Length) theFS.Close()
Dim theQuery As New OleDbCommand("INSERT INTO Table1([image]) VALUES(?)", new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\db1.mdb;User Id=admin;Password=;")) Dim p1 As New OleDbParameter("@p1", OleDbType.Binary) p1.Value = theBytes theQuery.Parameters.Add(p1) theQuery.Connection.Open() theQuery.ExecuteNonQuery() theQuery.Connection.Close()
|
Reading:
| | Dim theQuery As New OleDbCommand("SELECT ([image]) FROM Table1", New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\db1.mdb;User Id=admin;Password=;"))Dim theDataSet As New DataSet() Dim theDataAdapter As New OleDbDataAdapter(theQuery) theQuery.Connection.Open() theDataAdapter.Fill(theDataSet) theQuery.Connection.Close() 'get rowDim theItem As Object = theDataSet.Tables(0).Rows(0).Item(0) Dim correctObject() As Byte = CType(theItem, Byte()) If correctObject Is Nothing = False Then 'read into memstream Using theMemStream As New MemoryStream() theMemStream.Write(correctObject, 0, correctObject.Length) theMemStream.Position = 0
Me.PictureBox1.Image = Image.FromStream(theMemStream) End Using End If
|
does this help? What happens if you try this? what errors do you get?
since I unfortunately cannot edit my post (as the forums are down), I should also state that you of course need to modify the code to your needs (adding parameters, changing connection string etc....)
have you tried using the code I supplied here? I cannot seem to get that error at all - are you able to zip me up and send the project (with all files) via email? I'll be sure to post back here once I've found and cured the problem (email is in my profile, just click my name)
You just need to change your last 3 statements to the following:
Dim offseti As Integer = 0 'use 0 for non-bmp images or use 78 for bmp images
Dim strPic As New IO.MemoryStream
strPic.Write(bytPic, offseti, bytPic.Length- offseti)
picCover.Image = New Bitmap(strPic)
Hello, i'm having the same problem and this code simply doesn't work. I guess some images can't be loaded like this or maybe the method for loading the images from a memory stream has a limit, i'm not shure but this could be a reason.
Hello!
I have an OLE Object in my table, that may be jpg, doc, xml file.
In case of jpg I'd like to show it (VS2005 Visual basic).
I did next (not full code):Dim ms AsNew MemoryStream()
Dim reader As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess )
reader.Read()
retval = reader.GetBytes(0, 0, outbyte, 0, bufferSize)
ms.Write(outbyte, 0, retval)
ms.Seek(0, SeekOrigin.Begin)
curImage = Image.FromStream(ms)-> error "Parameter is not valid."I do not see the fault. Please help