Error " Invalid Parameter Used" when I try to display an image

G

Guest

I am trying to display images from an Access 2000 database and I get an error
"Invalid Parameter Used" when I execute the code line
"picBLOB.Image = Image.FromStream(stmBLOBData)" in my Visual Basic .Net
application.

I have researched MSDN for help and found the example article 321900 (see
below) and set up a test and everything works fine when I use SQL Server 2000
but when I modify the code and use data from Access 2000 using an
"OleDbDataAdapter" I get the error.

I tried a work around by importing my Access 2000 database into a SQL Server
data file and I get the same error using the sample code. If I load a picture
(jpg) into a SQL Server file and use the code sample it work but if I load
the same picture file into an Access 2000 file the code sample dosen't work.

What am I missing? Is There a parameter that I need when I use Access 2000?


How To Display an Image from a Database in a Windows Forms PictureBox by
Using Visual Basic .NET
Article ID : 321900 Last Review : July 15, 2004 Revision : 1.0
7. Add the following code in the Click event procedure of Button2:
Dim cn As New SqlConnection(strCn)
Dim cmd As New SqlCommand("SELECT BLOBID, " & _
"BLOBData FROM BLOBTest ORDER BY BLOBID", cn)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds, "BLOBTest")
Dim c As Integer = ds.Tables("BLOBTest").Rows.Count
If c > 0 Then
Dim bytBLOBData() As Byte = _
ds.Tables("BLOBTest").Rows(c - 1)("BLOBData")
Dim stmBLOBData As New MemoryStream(bytBLOBData)
picBLOB.Image = Image.FromStream(stmBLOBData)
End If
 
K

Kevin Yu [MSFT]

Hi,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you are trying to get a picture from
Access 2000 database according to a KB article. If there is any
misunderstanding, please feel free to let me know.

Now let's focus on getting data from Access 2000 instead of importing data
in SQL Server. First of all, please change al the Sql object to OleDb
object in the System.Data.OleDb namespace to support Access. Then try to
run that code again. The code seems to be fine, I think there might be some
mismatch with the database tables. Could you let me know on which line did
the app stop and throws the exception?

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Sorry the code that I included with my first post was the example that I
followed when I tested the SQL data. Here is the code that I'm using when I
try to display an image from Access 2000.

Dim iRowCount As Integer = 0
Dim c As Integer = 0

bmbRptData = Me.BindingContext(DsMyTest1, "MyTestData")
DsMyTest1.Clear()

iRowCount = OleDbDataAdapter1.Fill(DsMyTest1)

curRow = DsMyTest1.Tables("MyTestData").Rows(bmbRptData.Position)
c = bmbRptData.Position

curRow.BeginEdit()

MyPermit_No = curRow("Permit_No")

If c >= 0 Then
Try

Dim bytBLOBData() As Byte = _

DsMyTest1.Tables("MyTestData").Rows(c)("Sign_Photo")

Dim stmBLOBData As New MemoryStream(bytBLOBData)

' The code jumps to the exception catch with the
' ex3.message = "Invalid Parameter Used"
' when I run the next line

picBlobMyTest.Image = Image.FromStream(stmBLOBData)

Catch ex1 As ArgumentOutOfRangeException
MessageBox.Show(ex1.Message, "Argument error")
Catch ex2 As UnauthorizedAccessException
MessageBox.Show(ex2.Message, "UnauthorizedAccess error")
Catch ex3 As Exception
MessageBox.Show(ex3.Message, "An unhandled exception error")

Finally
 
K

Kevin Yu [MSFT]

Hi,

Thanks for you code to repro. I have made some trial on my machine to
reproduce the problem. Finally, when I tried to get pictures from northwind
database, Emplyees table, I get exactly the same exception as yours.

According to the KB article, this test does not work with the Photo column
in the Employees table of the sample Northwind database that is included
with Access and SQL Server. The bitmap images that are stored in the Photo
column are wrapped with the header information that the Visual Basic 6.0
OLE Container control creates.

So could you let me know, how the pictures were stored. I think it's the
byte array data that doesn't match the image object format causes the issue.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Kevin thanks for your help.

The Access database that I need to convert from is currently an Access 2000
database and the photo's were put into the database by dragging and dropping.
The users would drop any format that the original was in. The original data
goes back to 1997 and I have no idea how many Access versions have been used.

My test was on a empty Access 2000 database and I used the drag and drop
method to place a 3 jpg pictures and 1 bmp image into the database. What I
need to do is read the old database and save the old pictures to a file with
an Id that I can reference.

Thanks again for your input.
 
K

Kevin Yu [MSFT]

Hi,

If you have created the photos by dragging and dropping, I think this field
contains OLE header information. So we have to remove it before getting it
to a stream. I have tried the following code on Employees table, Northwind
database. It works well.

DataSet ds = new DataSet();
this.oleDbDataAdapter1.Fill(ds);
byte[] b = (byte[])ds.Tables[0].Rows[0]["Photo"];
MemoryStream m = new MemoryStream(b, 78, b.Length-78); //bmp file's
ole header is 78 bytes long.
Image i = Image.FromStream(m);
this.pictureBox1.Image = i;

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Kevin thanks for your suggestion.

Trimming 78 bytes from the beginning of the memory stream works for bmp
images in my file so there is a header record.
My Access file has a mixture of image types and when I tested a jpg record I
got the same error so a header record for a jpg image must be a different
length.

Do you know what the other image types header sizes are?
Is there a field in the header area to identify the image types?

Thanks again for your help.....
 
K

Kevin Yu [MSFT]

Hi,

Based on my experience, normally there are only two kinds of image object
which may be inserted into an Access field: the Bitmap image object (BMP
format) and the image document object (TIF format). In the header of the
file, there is string which records the picture's type. For Bitmap image
object, it is "Bitmap Image.Paint.Picture"; for image document object, it
is "Image Document.Imaging.Document". The position 20 is the first char of
the string. We can determine the type of the picture from it. 'B(66) means
Bitmap image object; 'I(73) means image document object. The image
document's header size is 4423 bytes.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Kevin thanks for the response.

I know that my users have dragged and dropped jpg files into the Access
database and I used thee jpg photos in my test database.

When I check position 20 on known jpg photo's I get 'P(80). If I continue
checking the next 31 bytes I get "Photo Editor Photo MSPhotoEd.3"

I have tried to use a header size of 4423 bytes but I get the "Invalid
parameter Used" error.

Do you have any idea's on what the header size should be?

Thanks again for your help....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top