0

My code

Building the column

IconColumn = new DataGridViewImageColumn()
{
    Name = "Icon",
    HeaderText = "Icon",
    SortMode = DataGridViewColumnSortMode.NotSortable,
    Width = 50,
    ImageLayout = DataGridViewImageCellLayout.Stretch,
    Resizable = DataGridViewTriState.False
};
IconColumn.DefaultCellStyle.NullValue = null;
IconColumn.CellTemplate = new ClockDataGridViewIconCell();

Setting an icon

float maxHeight = 200;
float maxWidth = 200;

var r = new Rectangle(0,
    0,
    (int)Math.Round(maxWidth),
    (int)Math.Round(maxHeight)
);
MyClockData.Icon = Utils.ResizeToFitBoundingBox(
    new Bitmap(fd.FileName),
    r);

The ResizeToFitBoundingBox method

internal static Bitmap ResizeToFitBoundingBox(Image image, in Rectangle box)
{
    float maxHeight = box.Width;
    float maxWidth = box.Height;

    float x = Math.Min(maxWidth / image.Width,
        maxHeight / image.Height);

    float newW = (float)image.Width * x;
    float newH = (float)image.Height * x;

    var bmp = new Bitmap((int)Math.Round(maxWidth),
        (int)Math.Round(maxHeight));
    bmp.MakeTransparent();
    using (Graphics gr = Graphics.FromImage(bmp))
    {
        gr.DrawImage(image, (bmp.Width - newW) / 2,
            (bmp.Height - newH) / 2, newW, newH);
    }

    return bmp;
}

Example icon

full-sized-icon

I have tried all the 4 possible values for DataGridViewImageColumn.ImageLayout and the cell looks the same:

  1. Normal

normal

  1. Not Set

not-set

  1. Stretch

stretch

  1. Zoom

zoom

None of them Works for what I wish. The official documentation is here. I would like the same behaviour as Forms.ImageLayout.Zoom.

Note: I use .NET Framework v4.6.1.

silviubogan
  • 3,343
  • 3
  • 31
  • 57
  • 1
    Zoom works well. You don't need any painting code. – Reza Aghaei Feb 13 '19 at 12:12
  • I tried a `DataGridViewImageColumn` having `ImageLayout` as `Zoom` and simply showed an image in the cell. It shows the zoomed image well without any problem. What's the role of resize function in your code, why do you need that? – Reza Aghaei Feb 13 '19 at 12:33
  • @RezaAghaei The role of the resize function is this: the images are chosen by the user and they must have a maximum resolution of 200x200. The function resizes them so the user can select an image of any resolution. – silviubogan Feb 13 '19 at 12:37
  • So the question doesn't have anything to do with `DataGridViewImageColumn`. If you pass a correct image the the column then the column will show it properly. Your question is probably just about implementing a function to change size of any image to at most 200*200 preserving the aspect ration. – Reza Aghaei Feb 13 '19 at 12:41

1 Answers1

0

I solved the problem by adding this line:

IconColumn.ImageLayout = DataGridViewImageCellLayout.Zoom;

after each statement which adds or updates an icon in the DataGridView.

It seems that setting this property to the same value as before repaints the icons in the column in the way the value of the property suggests.

silviubogan
  • 3,343
  • 3
  • 31
  • 57
  • It's really good news that you could solve the problem, but the question/answer are a bit confusing. You have told us *I have tried all the 4 possible values for DataGridViewImageColumn.ImageLayout and the cell looks the same*. Does this mean it was a a problem that can no longer be reproduced or a simple typographical error. If this is the case, it's better to close/delete the question. Otherwise I suggest you to edit the question as it's confusing and unclear now and I'm not sure you can achieve your goal (helping future readers). – Reza Aghaei Feb 13 '19 at 12:46
  • @RezaAghaei I updated the question and the answer. Thank you. – silviubogan Feb 13 '19 at 13:07