I'm using Winform DataGridView to display images. But when image fills cell, I don't see blue selection or in very low quantity. Please see:
When an cell is selected, I expect make cell whole transparent blue, not just sides or sides which isn't occupied by image. like:
Currently I tried coloring blue myself in paint event but it updates too frequently which hangs software.
I also modify image to look bluish in selection changed event, but again it slows down software.
Is there fix to this ? any workaround or something ? without compromising performance ?
EDIT: This is source code on how I display images on datagridview:
int colms = 4; // total no. of columns in our datagridview
//this create 4 image columns in datagridview
for (int c = 0; c < colms; c++)
{
var imgColm = new DataGridViewImageColumn();
imgColm.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
imgColm.ImageLayout = DataGridViewImageCellLayout.Zoom;
grid.Columns.Add(imgColm);
}
int colm = 0;
int row = 0;
//this get all images and display on datagridview
foreach (var img in Directory.GetFiles(@"C:\Users\Administrator\Desktop\images"))
{
if (colm >= colms)
{
row++;
colm = 0;
grid.Rows.Add();
}
((DataGridViewImageCell)grid.Rows[row].Cells[colm]).Value = Thumb.GetThumbnail(img, ThumbSize.LowRes);
colm++;
}
Currently cell painting I use just a workaround, that draws border on selected cell. But its slow when data is large and secondly draws on unselected cell as well.
Here's two examples to test yourself regarding the performance and the fill style of the selected cells. Since your code snippet does not show in what context the code is called, especially creating the image columns part, and to avoid repeating unnecessary routines, use the grid designer to add 4 columns of type
DataGridViewImageColumn
and set the auto size and layout properties from there.Normal Mode
In the Form's ctor, use
Reflection
to enable the grid'sDoubleBuffered
property to reduce the flicker. TheemptyImage
bitmap is the null value of the empty cells.Override the
OnLoad
method to populate the grid or to call a method for that.Note, your
Thumb.GetThumbnail
method returns a new image so you need to dispose of the original image.Implement the
CellPainting
event to draw everything except theDataGridViewPaintParts.SelectionBackground
and fill the selected cells with a semi-transparent color.Virtual Mode
You need here to have data store to cache only the images you need to display. The images of each cell of the visible rows. For that, The
Cache
class is created to manage the relevant functionalities including:SetMaxRows
method should be called when the grid is first created and resized to recalculate the visible rows.Dictionary<int, Image>
where the keys are the cell numbers.CellValueNeeded
event is raised.Finally, the
CellPainting
event remains almost the same except that you get the null image from thecache
instance.