vb.net Access 读写图片

前端之家收集整理的这篇文章主要介绍了vb.net Access 读写图片前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这个问题困扰我好长时间,网上找了很多资料,有用的不多,最终还是解决了这个问题,有类似问题的朋友可以参考一下,虽然代码写的不好


创建一个窗体,窗体上访两个按钮和一个图片框(PictureBox

图片写入到access是以byte数组的形式写入的,存储图片的重点是将图片转换成byte数组,网上有很多方法可以进行转换,经过测试下面的方式可以实现:Picturebyte = My.Computer.FileSystem.ReadAllBytes(PicturePath)

同样读取图片是将byte数组转换成图片,使用到下面的方法

Dim img As Byte() = CType(DT.Rows(0).Item("image"),Byte())
Dim pic As Image = Bitmap.FromStream(New IO.MemoryStream(img,True),True)

其中 DT 是从数据库中读出的数据



测试用表:表名:images 字段:no 自动编号,imgname 文本,image OLE


详细代码如下:

Imports System.Text
Imports System.Data.OleDb
Imports System.Data.Common
Imports System.IO
Public Class CsFrm
    Public Shared conString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=images.mdb;persist security info=false"
    Public Shared Myobject As Object
    Public Shared oboleconnection As OleDbConnection
    Public Shared oboleCommand As OleDbCommand
    Public Shared oboleadapter As OleDbDataAdapter

    Private Sub SaveBtn_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles SaveBtn.Click

        Dim Picturebyte() As Byte 'byte数组,用来暂时储存图片
        Dim PicturePath As String = "C:\MyImage.jpg" ' 图片的路径
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''
        '下面这句很重要,把图片转车byte数组,有很多种方法可以转换,经过测试,下面这种方法最合适
        Picturebyte = My.Computer.FileSystem.ReadAllBytes(PicturePath)
        Dim ssql As StringBuilder = New StringBuilder
        '数据库操作语句,  ? 是占位符
        ssql.Append(" insert into images([imgname],[image]) values(?,?);")
        oboleconnection = New OleDbConnection(conString)
        oboleCommand = New OleDbCommand
        Try
            oboleconnection.Open() '打开连接
            oboleCommand = New OleDb.OleDbCommand(ssql.ToString,oboleconnection)
            '替换掉占位符
            oboleCommand.Parameters.Add(New OleDb.OleDbParameter)
            oboleCommand.Parameters(0).Value = "名称"
            oboleCommand.Parameters.Add(New OleDb.OleDbParameter)
            oboleCommand.Parameters(1).Value = Picturebyte
            '执行数据库操作
            oboleCommand.ExecuteNonQuery()
            '弹出提示
            MsgBox("写入成功",MsgBoxStyle.Information)
        Catch ex As Exception
            MsgBox("写入失败",MsgBoxStyle.Information)
            'Throw New Exception(ex.Message)
        Finally
            oboleCommand.Dispose()
            oboleconnection.Close()
        End Try
    End Sub

    Private Sub ReadBtn_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles ReadBtn.Click

        Dim ssql As StringBuilder = New StringBuilder
        ssql.Append(" select  [imgname],[image] from images where [no] = 3;")
        Dim DT As DataTable
        oboleconnection = New OleDbConnection(conString)
        Try
            Dim DS As DataSet
            oboleconnection.Open()
            oboleadapter = New OleDbDataAdapter(ssql.ToString,oboleconnection)
            DS = New DataSet("DS")
            oboleadapter.Fill(DS) '使用 Fill 将数据从数据源加载到 DataSet 中

            DT = DS.Tables(0)
            '''''''''''''''''''''''''''''''''''''''''''''''
            ''''''下面两句是关键 先把读出的数据转换成byte数组,然后转换成图像
            Dim img As Byte() = CType(DT.Rows(0).Item("image"),Byte())
            Dim pic As Image = Bitmap.FromStream(New IO.MemoryStream(img,True)
            PictureBox1.Image = pic
        Catch ex As Exception
            'Throw New Exception(ex.Message)
        Finally
            oboleconnection.Dispose()
            oboleconnection.Close()
        End Try
    End Sub
End Class
原文链接:https://www.f2er.com/vb/258258.html

猜你在找的VB相关文章