user2715202
user2715202

Reputation: 43

"System.Drawing.Image" cannot be converted to '1-dimensional array

I'm making a registration form with a picture in VB.Net using an Access database. Utilizing the following code, I am getting an error.

Me.Student_instructorTableAdapter.Insert(txtname,txtpass, ***PictureBox1.Image***)
Me.Student_instructorTableAdapter.Fill(Me.DatabaseDataSet4.student_instructor)

MsgBox("Successfully added", MsgBoxStyle.Information)

The PictureBox1.Image is saving into an Access Database ("picture") which throws an error:

"System.Drawing.Image" cannot be converted to '1-dimensional array"

What should I use instead of PictureBox1.Image when I call Insert in order to avoid the exception?

Upvotes: 0

Views: 2004

Answers (1)

Basic
Basic

Reputation: 26766

(Should be a comment but I need to post code)

We need the full exception message to know exactly what it's expecting (1-dimensional array of what?) Most likely it's a Byte array.

If so, you can convert an image to a Byte array like this...

Public Function ImageToByteArray(imageIn As System.Drawing.Image) As Byte()
    Dim ms As New MemoryStream()
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
    Return ms.ToArray()
End Function

You can convert a Byte array back to an image like this...

Public Function byteArrayToImage(byteArrayIn As Byte()) As Image
    Dim ms As New MemoryStream(byteArrayIn)
    Dim returnImage As Image = Image.FromStream(ms)
    Return returnImage
End Function

See here for more information

I'm not familiar with access data types so it may support images directly. If so, you need to make sure that you've got the right data type selected for that column/field.

Incidentally, if you don't have to, don't use Access (or at least the storage engine/JET) for new projects - it's slow, unreliable with more than 10 users and has serious security issues. If you don't want a full-up SQL Server database, consider SQL Compact Edition (CE) or SQL Express.

Upvotes: 2

Related Questions