如何从Ms访问Visual Studio 2012中获取图片以显示 [英] How to fetch the picture from Ms access to Visual studio 2012 to display
问题描述
我是视觉工作室的新手。我使用Visual Studio 2012创建了一个测验项目,其中包括用户登录检查MS访问,洗牌问题和访问选项,将分数保存到访问。所以我想在项目中前进我需要在我的项目中添加图片。我从未尝试从访问中获取图片并以vb格式显示。我收到此错误( 无法将类型为'System.Byte []'的对象转换为在尝试下面的代码时键入'System.Drawing.Image' )我只给出了图片代码:
I am new to visual studio.I have created a quiz project using Visual Studio 2012 which includes user login from checking with MS access ,shuffled questions and options from access, saving score to access.So I thought to advance in the project.I need to add picture in my project.I have never tried fetching picture from access and displaying in vb form.I am getting this error(Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image') while trying the code below I have given only picture codes:
<---------------Declaration---------------->
Dim conn As OleDbConnection
Dim com As OleDbCommand
Dim reader As OleDbDataReader
Dim Str As String
Dim ques(10) As Image
Dim CorAns(10) As String
Public count As Integer = 0
Dim i As Integer
<--------fetching data from database and storing in local variable----->
conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\VB\quiz.accdb")
conn.Open()
Str = "select * from table1"
com = New OleDbCommand(Str, conn)
reader = com.ExecuteReader
i = 1
While (reader.Read())
ques(i) = reader("Ques")
CorAns(i) = reader("Answer").ToString
i += 1
End While
conn.Close()
reader.Close()
<------displaying it in picture box from local variable-------->
PictureBox1.Image = ques(i)
推荐答案
首先尝试将数据放入内存流中,例如
Try putting the data into a memorystream first e.g.
Dim ms As MemoryStream = new MemoryStream(reader("Ques"))
ques(i) = Image.FromStream(ms)
警告 - 我没试过这个在VB中。您可能需要将 reader(Ques)
明确分配给<code> Byte() first
Caveat - I haven't tried this in VB. You may need to assign reader("Ques")
explicitly to a Byte()
first
看看这个:为什么我得到参数无效。我从数据库中读取图像时出现异常? [ ^ ] - 它比你想象的要复杂得多,特别是因为你是初学者,很有可能你没有保存数据库的正确信息。
代码在C#而不是VB,但原理是相同的,这将帮助您转换它:代码转换器 [ ^ ]
Have a look at this: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - it's a little more complicated than you thought, particularly as you are a beginner, and the chances are that you have not saved the right information to the DB either.
The code is in C# not VB, but the principle is the same and this will help you convert it: Code Converter[^]
使用此代码将图像保存在数据库中
use this code to save your image in the database
Try
ConnDB() 'function to database database connection which include myConnection as a connection string
Dim command As New OleDbCommand("UPDATE tableName SET columnName= @Picture WHERE ID=1", myConnection)
'Create an empty stream in memory.
Dim img As New Bitmap(pictureBox1.Image)
Using stream As New IO.MemoryStream
'Fill the stream with the binary data from the Image.
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png)
Dim bytImage() As Byte = stream.GetBuffer
'Get an array of Bytes from the stream and assign to the parameter.
command.Parameters.Add("@Picture", OleDbType.VarBinary).Value = bytImage
End Using
myConnection.Open()
command.ExecuteNonQuery()
myConnection.Close()
Catch ex As Exception
'catch the exceptions and close the connection
MessageBox.Show(ex.Message)
myConnection.Close()
End Try
并使用它来检索图片框中的图像
and use this to retrieve the image in the picturebox
Try
ConnDB()
Dim stream As New MemoryStream()
myConnection.Open()
Dim command As New OleDbCommand("select columnName from tableName where id=1", myConnection)
Dim image As Byte() = DirectCast(command.ExecuteScalar(), Byte())
stream.Write(image, 0, image.Length)
myConnection.Close()
Dim bitmap As New Bitmap(stream)
bitmap.MakeTransparent(Color.Black)
pictureBox2.Image = bitmap
Catch ex As Exception
MsgBox(ex.Message)
myConnection.Close()
End Try
i希望这个很有帮助
i Hope this is helpful
这篇关于如何从Ms访问Visual Studio 2012中获取图片以显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!