datagridview中显示图片

来源:互联网 发布:水乳精华 知乎 编辑:程序博客网 时间:2024/04/29 11:25

 呵呵,干脆把datagridview中显示图片的也写下来。

像绑定之类的和数据库中存放的为二进制的就不多讲了,就贴出图片名称到图片显示的转换吧!

    Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
        If DataGridView1.Columns(e.ColumnIndex).Name.Equals("PhotoTest") Then
            Try
                Dim path As String
                path = Application.StartupPath & "/" & DataGridView1.Rows(e.RowIndex).Cells(3).Value.ToString & ".bmp"   '取出图片路径,从datagridview中获取图片名称
                e.Value = getImage(path)
            Catch ex As Exception
            End Try
        End If

    End Sub

    Private Function getImage(ByVal path As String)
        Dim files As FileStream = New FileStream(path, FileMode.Open)
        Dim pic As Image = Image.FromStream(files)
        files.Close()
        Return pic
    End Function

 

//改下getImage方法,下为C#的

 //根据路径获取图片
        private Image getImage(string imgagePath)
        {
            FileStream files = new FileStream(imgagePath, FileMode.Open);
            Image pic = Image.FromStream(files);
            files.Close();
            return pic;
        }

原创粉丝点击