VB.NET中DataGridView RowPostPaint行头图像模糊及白边问题求助
VB.NET DataGridView行头图像模糊、白边问题解决
问题说明
在VB.NET中通过DataGridView的RowPostPaint事件绘制行头图像时,出现图像模糊、图像两侧有白边的问题,而相同图像在PictureBox中显示正常,需要实现一致的显示效果。
现有代码
初始代码
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
更新代码
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
效果对比
- 初始效果:

- PictureBox参考效果:

- 更新代码后效果:

- 目标效果:

问题原因与修正方案
核心问题分析
- Icon转换失真:将Bitmap转为Icon时,Icon有固定尺寸限制,会强制缩放图像导致细节丢失,引发模糊。
- 绘图质量未优化:默认Graphics未启用高质量插值模式,缩放图像时算法粗糙,导致模糊。
- 位置计算错误:原位置计算未考虑图像宽度,导致图像偏移,出现两侧白边。
修正后的代码
Private Sub DataGridView1_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint '直接使用ImageList中的原始图像,避免不必要的格式转换 Dim sourceImage As Image = imageList1.Images(0) Dim graphics As Graphics = e.Graphics '开启高质量渲染模式,提升图像清晰度 graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality graphics.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality '按原始图像比例计算目标尺寸,避免拉伸变形 Dim targetWidth As Integer = 20 Dim targetHeight As Integer = CInt(sourceImage.Height * (targetWidth / CDbl(sourceImage.Width))) '精确计算居中位置,确保图像完全填充无偏移 Dim xPos As Integer = e.RowBounds.X + (DataGridView1.RowHeadersWidth - targetWidth) \ 2 Dim yPos As Integer = e.RowBounds.Y + (DataGridView1.Rows(e.RowIndex).Height - targetHeight) \ 2 Dim drawRect As New Rectangle(xPos, yPos, targetWidth, targetHeight) '绘制图像 graphics.DrawImage(sourceImage, drawRect) End Sub
关键修正点
- 跳过Bitmap转Icon的步骤,直接使用ImageList中的原始图像,避免格式转换带来的失真。
- 配置Graphics的高质量渲染参数,让图像缩放时保持清晰。
- 按原始图像的宽高比计算目标尺寸,防止图像拉伸变形。
- 修正位置计算逻辑,确保图像在行头单元格中完全居中,消除白边。
内容的提问来源于stack exchange,提问作者dlaksmi
相关产品推荐
相关产品推荐

