Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How to load different images in imagecolumn
 

How to load different images in imagecolumn

Hi All,

I have created a windows forms application and using datagridView in one of the forms. I need to add a new Image column dynamically( the image is already availble in the datasource) and just compress it's size and displaying in the new column.

I found that it's always displaying the same image through out all the rows. I need different images in each of the rows.Is it possible? Please find the source code and advice.

            DataGridViewImageColumn imgCol = new DataGridViewImageColumn();
            {
                imgCol.CellTemplate = new DataGridViewImageCell();
                imgCol.Width = 100;
                imgCol.Name = "imgThumb";
                imgCol.HeaderText = "Thumb Image";

            }
            grdVisitor.Columns.Add(imgCol);
            
            foreach (DataGridViewRow rw in grdVisitor.Rows)
            {
                byte[] bytImg = (byte[])grdVisitor.Rows[rw.Index].Cells["Image"].Value;
                MemoryStream ms = new MemoryStream(bytImg);
                Image img = Image.FromStream(ms);

                int scaledY = Math.Min(100, img.Height / (img.Width / 100));
                int scaledX = Math.Min(100, img.Width / (img.Height / 100));

                grdVisitor.Rows[rw.Index].Cells["Image"].Value = img.GetThumbnailImage(scaledX, scaledY, null, System.IntPtr.Zero);
                imgCol.Image = img.GetThumbnailImage(scaledX, scaledY, null, System.IntPtr.Zero);
            }<br/>
I have written this in the Form_load Event. Kindly let me know how to achieve a different image in each row.

Thanks
Pad M Lingam  Thursday, July 23, 2009 5:58 AM

Addd CellFormatting Event for DataGridView and use following code in handler

private void grdVisitor_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (grdVisitor.Columns[e.ColumnIndex].Name == "imgThumb")
{
byte[] bytImg = (byte[])grdVisitor.Rows[e.RowIndex].Cells["Image"].Value;
MemoryStream ms = new MemoryStream(bytImg);
Image img = Image.FromStream(ms);

int scaledY = Math.Min(100, img.Height / (img.Width / 100));
int scaledX = Math.Min(100, img.Width / (img.Height / 100));

e.Value = img.GetThumbnailImage(scaledX, scaledY, null, System.IntPtr.Zero);
}

}

Modify the code according to your need because i just written it on the fly.


-@SuDhiR@-
  • Marked As Answer byPad M Lingam Thursday, July 23, 2009 7:51 AM
  •  
_SuDhiR_  Thursday, July 23, 2009 6:59 AM
You probablly have to do that in the RowPrePaint or CellFormatting event handler. There you have to take care that you only execute the code if really necessary.


--
Wolfgang
Wollinet  Thursday, July 23, 2009 6:14 AM

Addd CellFormatting Event for DataGridView and use following code in handler

private void grdVisitor_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (grdVisitor.Columns[e.ColumnIndex].Name == "imgThumb")
{
byte[] bytImg = (byte[])grdVisitor.Rows[e.RowIndex].Cells["Image"].Value;
MemoryStream ms = new MemoryStream(bytImg);
Image img = Image.FromStream(ms);

int scaledY = Math.Min(100, img.Height / (img.Width / 100));
int scaledX = Math.Min(100, img.Width / (img.Height / 100));

e.Value = img.GetThumbnailImage(scaledX, scaledY, null, System.IntPtr.Zero);
}

}

Modify the code according to your need because i just written it on the fly.


-@SuDhiR@-
  • Marked As Answer byPad M Lingam Thursday, July 23, 2009 7:51 AM
  •  
_SuDhiR_  Thursday, July 23, 2009 6:59 AM
Hi Sudhir,

Thanks for your reply. It worked.

Thanks.
Pad M Lingam  Thursday, July 23, 2009 7:51 AM

You can use google to search for other answers

Custom Search

More Threads

• Remove rows in datagridview and save the dataset xml file
• Inside Cell ??? I need help
• syntax
• DataGridView VerticalScrollbar Member
• BindingSource.AddNew resulting in InvalidCastException
• change backcolour of particular datagrid cell depending upon condition
• How to delete multiple selected rows from a unbound datagridview
• passing values to parameters to create typed dataset
• Typed DataSet and WriteXml question about Namespace
• Problem with VS2005 TableAdapter Designer and DateTime functions