为什么 vb.net 中行标题中的事件 datagridview RowPostPaint 输出图像变得模糊

问题描述 投票:0回答:1

我正在尝试使用事件 datagridview RowPostPaint,但行标题中的输出图像变得模糊,并且 vb.net 图像的每一侧都有白色。

如果你看截图,图片框中出现了好的图像结果,我怎样才能得到与图片框相同的结果。或者我的代码有问题。

请指导我

谢谢

Public Class Form1

    Private _source As New BindingSource()
    Private _person As List(Of person) = Nothing

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        _person = New List(Of person)()
        _person.Add(New person With {
            .ID = 1,
            .Name = "Tridip"
        })
        _person.Add(New person With {
            .ID = 2,
            .Name = "Sujit"
        })
        _person.Add(New person With {
            .ID = 3,
            .Name = "Arijit"
        })
        _source.DataSource = _person
        DataGridView1.DataSource = _source

    End Sub
    Private Sub DataGridView1_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
        'Convert the image to icon, in order to load it in the row header column
        Dim myBitmap As New Bitmap(imageList1.Images(0))
        Dim myIcon As Icon = Icon.FromHandle(myBitmap.GetHicon())

        Dim graphics As Graphics = e.Graphics

        'Set Image dimension - User's choice
        Dim iconHeight As Integer = 20
        Dim iconWidth As Integer = 20

        'Set x/y position - As the center of the RowHeaderCell
        Dim xPosition As Integer = e.RowBounds.X + (DataGridView1.RowHeadersWidth / 2)
        Dim yPosition As Integer = e.RowBounds.Y + ((DataGridView1.Rows(e.RowIndex).Height - iconHeight) \ 2)

        Dim rectangle As New Rectangle(xPosition, yPosition, iconWidth, iconHeight)
        graphics.DrawIcon(myIcon, rectangle)

    End Sub
End Class


Public Class person
    Public Property ID As Integer
    Public Property Name As String
End Class


screenhot

checklist

更新代码

Private Sub DataGridView1_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
        'Convert the image to icon, in order to load it in the row header column
        Dim myBitmap As New Bitmap(imageList1.Images(0))
        Dim myIcon As Icon = Icon.FromHandle(myBitmap.GetHicon())

        Dim graphics As Graphics = e.Graphics

        'Set Image dimension - User's choice
        Dim iconHeight As Integer = 20
        Dim iconWidth As Integer = 20

        'Set x/y position - As the center of the RowHeaderCell
        Dim xPosition As Integer = e.RowBounds.X + (DataGridView1.RowHeadersWidth / 2)
        Dim yPosition As Integer = e.RowBounds.Y + ((DataGridView1.Rows(e.RowIndex).Height - iconHeight) \ 2)

        Dim rectangle As New Rectangle(xPosition, yPosition, iconWidth, iconHeight)
        'graphics.DrawIcon(myIcon, rectangle)
        graphics.DrawImage(myBitmap, rectangle)
    End Sub

更新代码的结果

result from update code

vb.net image winforms graphics datagridview
1个回答
0
投票

看来您已将

28x28
PNG 图像插入到
ImageList
中并设置了默认属性。根据
ColorDepth
ImageSize
属性调整图像大小并重新采样。结果,图像变得更小且质量更低。

您不需要将图像放在

ImageList
中。将其包含在项目的
Resources
中以保持原样,并设置相关的
Graphics
属性以产生高质量的收缩和变换。

在项目的属性中,打开资源设计器并放置图像。在代码中,声明一个类型为

Bitmap
的类变量并将图像分配给它。实现网格的
CellPainting
事件如下...

Public Class Form1
    Private ReadOnly checkMarkImage As Bitmap

    Sub New()
        InitializeComponent()
        checkMarkImage = My.Resources.CheckMark
    End Sub

   Private Sub DataGridView1_CellPainting(
       sender As Object,
       e As DataGridViewCellPaintingEventArgs) Handles _
       DataGridView1.CellPainting
       Dim dgv = DirectCast(sender, DataGridView)

       e.Paint(e.ClipBounds, DataGridViewPaintParts.All)

       If e.RowIndex >= 0 AndAlso
           e.ColumnIndex < 0 AndAlso
           e.RowIndex <> DataGridView1.NewRowIndex AndAlso
           String.IsNullOrEmpty(dgv.Rows(e.RowIndex).ErrorText) Then
           Dim gs = e.Graphics.Save()
           Dim sz = DataGridView1.RowTemplate.Height - 4 ' Change as needed...
           Dim srcRect = New Rectangle(Point.Empty, checkMarkImage.Size)
           Dim destRect = New Rectangle(
               e.CellBounds.Right - sz - 1,
               e.CellBounds.Y + (e.CellBounds.Height - sz) \ 2,
               sz, sz)

           ' High-quality shrinking if needed...
           e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic
           ' Note the white checkmark with and without this one...
           e.Graphics.CompositingQuality = CompositingQuality.HighQuality
           e.Graphics.DrawImage(checkMarkImage, destRect, srcRect, GraphicsUnit.Pixel)
           e.Graphics.Restore(gs)
           e.Handled = True
       End If
   End Sub
End Class
© www.soinside.com 2019 - 2024. All rights reserved.