2014年3月3日 星期一

關於DataGridView中嵌入圖片處理

參考部份引用來源
--
重點在 ##3 這段的處理 (縮圖)

##1
Public Partial Class Form1
    Inherits Form
    Private dgv As DataGridView = Nothing

    Public Sub New()
        InitializeComponent()

        '初始化动态产生数据表等字段
        dgv = New DataGridView()
        dgv.Parent = Me
        dgv.Dock = DockStyle.Fill

        Dim dt As New DataTable()
        dt.Columns.Add("Name", GetType(String))
        dt.Columns.Add("Image", GetType(Byte()))
        dt.Rows.Add("Test1", File.ReadAllBytes("d:\test.jpg"))
        dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
        dgv.DataSource = dt
    End Sub
End Class

##2
Public Partial Class Form1
    Inherits Form
    Private dgv As DataGridView = Nothing

    Public Sub New()
        InitializeComponent()

        '初始化动态产生数据表等字段
        dgv = New DataGridView()
        dgv.Parent = Me
        dgv.Dock = DockStyle.Fill

        Dim dt As New DataTable()
        dt.Columns.Add("Name", GetType(String))
        dt.Columns.Add("Image", GetType(String))
        dt.Rows.Add("Test1", "d:\test.jpg")
        dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
        dgv.Columns.Add(New DataGridViewTextBoxColumn() With { _
            Key .HeaderText = "Name", _
            Key .DataPropertyName = "Name" _
        })
        dgv.Columns.Add(New DataGridViewImageColumn() With { _
            Key .HeaderText = "Image", _
            Key .DataPropertyName = "Image" _
        })
        AddHandler dgv.CellFormatting, AddressOf New DataGridViewCellFormattingEventHandler(AddressOf dgv_CellFormatting)
        dgv.DataSource = dt
    End Sub

    Private Sub dgv_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs)
        If e.ColumnIndex = 1 AndAlso e.RowIndex <> dgv.NewRowIndex Then
            e.Value = File.ReadAllBytes(dgv.Rows(e.RowIndex).Cells(1).Value.ToString())
        End If
    End Sub
End Class

##3
Public Partial Class Form1
    Inherits Form
    Private dgv As DataGridView = Nothing

    Public Sub New()
        InitializeComponent()

        '初始化动态产生数据表等字段
        dgv = New DataGridView()
        dgv.Parent = Me
        dgv.Dock = DockStyle.Fill

        Dim dt As New DataTable()
        dt.Columns.Add("Name", GetType(String))
        dt.Columns.Add("Image", GetType(Byte()))
        dt.Rows.Add("Test1", File.ReadAllBytes("d:\test.jpg"))
        dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
        AddHandler dgv.CellFormatting,AddressOf New DataGridViewCellFormattingEventHandler(AddressOf dgv_CellFormatting)
        dgv.AllowUserToAddRows = False
        dgv.DataSource = dt
    End Sub

    Private Sub dgv_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs)
        If e.ColumnIndex = 1 Then
            Using oldms As New MemoryStream(DirectCast(e.Value, Byte()))
                Dim img As Image = Image.FromStream(oldms)
                Dim bt As New Bitmap(img, New Size(100, 100))
                Using newms As New MemoryStream()
                    bt.Save(newms, ImageFormat.Jpeg)
                    e.Value = newms.ToArray()
                End Using
            End Using
        End If
    End Sub
End Class

##4
Public Partial Class Form1
    Inherits Form
    Private dgv As DataGridView = Nothing

    Public Sub New()
        InitializeComponent()

        '初始化动态产生数据表等字段
        dgv = New DataGridView()
        dgv.Parent = Me
        dgv.Dock = DockStyle.Fill

        Dim dt As New DataTable()
        dt.Columns.Add("Name", GetType(String))
        dt.Columns.Add("Image", GetType(String))
        dt.Rows.Add("Test1", "d:\test.jpg")
        dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells

        dgv.Columns.Add(New DataGridViewTextBoxColumn() With { _
            Key .HeaderText = "Name", _
            Key .DataPropertyName = "Name" _
        })
        dgv.Columns.Add(New DataGridViewImageColumn() With { _
            Key .HeaderText = "Image", _
            Key .DataPropertyName = "Image" _
        })
        AddHandler dgv.CellFormatting,AddressOf New DataGridViewCellFormattingEventHandler(AddressOf dgv_CellFormatting)
        dgv.AllowUserToAddRows = False
        dgv.DataSource = dt
    End Sub

    Private Sub dgv_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs)
        If e.ColumnIndex = 1 Then
            Dim bytes As Byte() = File.ReadAllBytes(e.Value.ToString())
            Using oldms As New MemoryStream(bytes)
                Dim img As Image = Image.FromStream(oldms)
                Dim bt As New Bitmap(img, New Size(100, 100))
                Using newms As New MemoryStream()
                    bt.Save(newms, ImageFormat.Jpeg)
                    e.Value = newms.ToArray()
                End Using
            End Using
        End If
    End Sub
End Class

沒有留言:

張貼留言