加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

VB.NET对图片读取操作

发布时间:2020-12-17 07:52:21 所属栏目:百科 来源:网络整理
导读:VB.NET对图片的操作,SQL数据库中存取字段类型Image,下面对图片读取代码: 界面如下: 1、 上传按钮代码 Dim L_Err As String = "" Dim opd As OpenFileDialog = New OpenFileDialog() opd.CheckFileExists = True opd.CheckPathExists = True opd.RestoreD
VB.NET对图片的操作,SQL数据库中存取字段类型Image,下面对图片读取代码:

界面如下:









1、 上传按钮代码

   Dim L_Err As String = ""
        Dim opd As OpenFileDialog = New OpenFileDialog()
        opd.CheckFileExists = True
        opd.CheckPathExists = True
        opd.RestoreDirectory = True
        opd.DefaultExt = "*.*"
        opd.Filter = "图像文件 (*.bmp;*.gif;*.jpg;*.jpeg;*.png)|*.bmp;*.gif;*.jpg;*.jpeg;*.png"
        opd.ShowDialog()
        If opd.FileName <> "" Then
            Me.PicBox.Image = Image.FromFile(opd.FileName)
            Me.PicBox.ImageLocation = opd.FileName
        End If
2、确定按钮代码
     If ToStr(Me.PicBox.ImageLocation).Trim() <> "" Then
            Try
                Dim sSql1 As String = ""
               'sSql1 = "insert into U_CallInfoSet(backImage) values(@fs)"  //插入或者更新语句
                sSql1 = "update U_CallInfoSet set backImage=@fs"
                SqlHelper.ExecuteSqlWithImg(sSql1,My.Computer.FileSystem.ReadAllBytes(Me.PicBox.ImageLocation))
             
            Catch ex As Exception
                MessageBox.Show(ex.ToString())
            End Try
        End If

3、界面加载事件显示

'取得数据库字段 dt.Rows(0)("Pic")

     If  dt.Rows(0)("Pic") Is DBNull.Value Then
        Me.PicBox.Image = Nothing
     Else
        Me.PicBox.Image = GetImage(dt.Rows(0)("Pic"))
     End If
4、需要用到的方法
    ''' <summary>
    ''' 字节数组转换为Image类型
    ''' </summary>
    ''' <param name="bData"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetImage(ByVal bData As Byte()) As Image
        Try
            Using fStream As Stream = New MemoryStream(bData.Length)
                Dim bWriter As New BinaryWriter(fStream)
                bWriter.Write(DirectCast(bData,Byte()))
                bWriter.Flush()
                Dim bitMap As New System.Drawing.Bitmap(fStream)
                bWriter.Close()
                fStream.Close()
                Dim iImage As Image = System.Drawing.Image.FromHbitmap(bitMap.GetHbitmap())
                Return iImage
            End Using
        Catch e As System.IO.IOException
            Throw New Exception(e.Message & "Read image data error!")
        End Try
    End Function

    ''' <summary>
    ''' 处理SQL中操作Image类型
    ''' </summary>
    ''' <param name="strSQL">SQL语句</param>
    ''' <param name="fs">图像字节,数据库的字段类型为image的情况</param>
    ''' <returns>影响的记录数</returns>
    Public Shared Function ExecuteSqlWithImg(ByVal strSQL As String,ByVal fs As Byte()) As Integer
        Dim strCon As String = "Data Source=xxx;Initial Catalog=temp;User ID=sa;Password=123"
        Using connection As New SqlConnection(strCon)
            Dim cmd As New SqlCommand(strSQL,connection)
            Dim myParameter As New System.Data.SqlClient.SqlParameter("@fs",SqlDbType.Image)
            myParameter.Value = fs
            cmd.Parameters.Add(myParameter)
            Try
                connection.Open()
                Dim rows As Integer = cmd.ExecuteNonQuery()
                Return rows
            Catch e As System.Data.SqlClient.SqlException
                Throw e
            Finally
                cmd.Dispose()
                connection.Close()
            End Try
        End Using
    End Function
OK!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读