I'm trying to draw an image in the DataGridView header by text's side. I can draw the imagem but it loss resolution. Why?
before:
after (on the DataGridView header):
I tried to resize it but it doesn't make difference. Also, I want to keep the header's text and place the image on text's right side. How do I do that?
I'm doing the following:
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;
}
So my question is: how do I draw the image so that it doesn't loss resolution and keep the header's text?