C# 通过文本';s侧

C# 通过文本';s侧,c#,image,winforms,datagridview,C#,Image,Winforms,Datagridview,我试图在DataGridView标题的文本侧边绘制一幅图像。我能画出这张图,但分辨率不高。为什么? 之前: 之后(在DataGridView标题上): 我试图调整它的大小,但没有什么区别。另外,我想保留标题的文本,并将图像放在文本的右侧。我该怎么做 我正在做以下工作: private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {

我试图在DataGridView标题的文本侧边绘制一幅图像。我能画出这张图,但分辨率不高。为什么?

之前:

之后(在DataGridView标题上):

我试图调整它的大小,但没有什么区别。另外,我想保留标题的文本,并将图像放在文本的右侧。我该怎么做

我正在做以下工作:

   private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex == -1)
            {

                // original image size is 96x96 px
                Image img = ScaleImage(imageList1.Images[0], 32, 32);
                var s = e.CellBounds;
                s.Height = img.Height;
                s.Width = img.Width;
                e.Paint(s, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground);
                e.Graphics.DrawImage(img, s);
                e.Handled = true;
            }
        }


    //code taken from: http://stackoverflow.com/questions/6501797/resize-image-proportionally-with-maxheight-and-maxwidth-constraints
    // all credits to @Alex Aza
    public Image ScaleImage(Image image, int maxWidth, int maxHeight)
    {
        var ratioX = (double)maxWidth / image.Width;
        var ratioY = (double)maxHeight / image.Height;
        var ratio = Math.Min(ratioX, ratioY);

        var newWidth = (int)(image.Width * ratio);
        var newHeight = (int)(image.Height * ratio);

        var newImage = new Bitmap(newWidth, newHeight);
        Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
        return newImage;
    }

因此,我的问题是:如何绘制图像,使其不会丢失分辨率并保留标题文本?

我猜您的DataGridViewImageColumn的ImageLayout属性设置错误。

您可以对
DataGridView DGV使用类似的内容:

private void DGV_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.ColumnIndex == yourColumn && e.RowIndex == -1)
    {
       DGV.ColumnHeadersHeight = 32;  // or maybe a little more..
       // should be prepared, maybe in an imagelist!!
       Image img = Image.FromFile("D:\\dollars96.png");
       Rectangle r32 = new Rectangle(e.CellBounds.Left + e.CellBounds.Width - 32, 0, 32,32);
       Rectangle r96 = new Rectangle(0, 0, 96,96);
       string header = DGV.Columns[e.ColumnIndex].HeaderText;
       e.PaintBackground(e.CellBounds, true);  // or maybe false ie no selection?
       e.PaintContent(e.CellBounds);  

       e.Graphics.DrawImage(img, r32, r96, GraphicsUnit.Pixel);

       e.Handled = true;
    }
    // any other cell: let the system do its thing
    else e.Handled = false;
}


您可能需要检查
图像列表的设置
;默认设置的质量相当低!。顺便说一句,您可以将其设置为
32x32
。请升级
颜色深度

我没有一套。这与列中的图像有关,不是吗?我想把它们放在标题中你的列标题看起来不对。设置ColumnHeaderShightSize=DisableResizing并设置ColumnHeaderShight=32。我认为OP使用ImageList是罪魁祸首,我一直避免使用它。@Lars:你是什么意思?他们看起来完全像他们应该做的,就像没有我画的那幅画的定制画一样。ColumnHeaders是在我的代码中设置的,我已经注意到ImageList的问题。。(您可能已经看到了我的答案的早期版本?)我在您编辑ImageList之前发表了评论。你的字体大小可能会让我很反感。:-)