Click here to Skip to main content
15,843,727 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I am having trouble with displaying images in my picture box. I open a file and fill my datagridview with images from a folder. I want that every time the user makes a selection of an image from the gridview it will be displayed in the picturebox. I have tried different ways but i get an error of cannot change to image. Any help will be great.

The following is the code for loading the images into the gridview:
private void LoadImages()
        {
            try
            {
                if (_files == null)
                {
                    return;
                }

                if (this.WindowState == FormWindowState.Minimized)
                {
                    return;
                }

                dataViewImages.Rows.Clear();
                dataViewImages.Columns.Clear();

                int numColumnsForWidth = (dataViewImages.Width - 5) / (_imageSize + 20);
                int numRows = 0;

                int numImagesRequired = 0;

                if (_currentEndImageIndex > _files.Count)
                {
                    // Are we requesting to display more images than we actually have? If so then reduce
                    if (_currentStartImageIndex == 0)
                    {
                        numImagesRequired = _files.Count;
                    }
                    else
                    {
                        numImagesRequired = (_currentEndImageIndex - _currentStartImageIndex) - (_currentEndImageIndex - _files.Count);
                    }
                }
                else
                {
                    // Calculated the number of rows we will need for normal use
                    numImagesRequired = _currentEndImageIndex - _currentStartImageIndex; 
                }

                numRows = numImagesRequired / numColumnsForWidth;

                // Do we have a an overfill for a row
                if (numImagesRequired % numColumnsForWidth > 0)
                {
                    numRows += 1;
                }

                // Catch when we have less images than the maximum number of columns for the DataGridView width
                if (numImagesRequired < numColumnsForWidth)
                {
                    numColumnsForWidth = numImagesRequired;
                }

                int numGeneratedCells = numRows*numColumnsForWidth;

                // Dynamically create the columns
                for (int index = 0; index < numColumnsForWidth; index++)
                {
                    DataGridViewImageColumn dataGridViewColumn = new DataGridViewImageColumn();

                    dataViewImages.Columns.Add(dataGridViewColumn);
                    dataViewImages.Columns[index].Width = _imageSize + 20;
                }

                // Create the rows
                for (int index = 0; index < numRows; index++)
                {
                    dataViewImages.Rows.Add();
                    dataViewImages.Rows[index].Height = _imageSize + 20;
                }

                int columnIndex = 0;
                int rowIndex = 0;

                for (int index = _currentStartImageIndex; index < _currentStartImageIndex + numImagesRequired; index++)
                {
                    // Load the image from the file and add to the DataGridView
                    Image image = Helper.ResizeImage(_files[index], _imageSize, _imageSize, false);
                    dataViewImages.Rows[rowIndex].Cells[columnIndex].Value = image;
                    dataViewImages.Rows[rowIndex].Cells[columnIndex].ToolTipText = Path.GetFileName(_files[index]);

                    // Have we reached the end column? if so then start on the next row
                    if (columnIndex == numColumnsForWidth - 1)
                    {
                        rowIndex++;
                        columnIndex = 0;
                    }
                    else
                    {
                        columnIndex++;
                    }
                }

                // Blank the unused cells
                if (numGeneratedCells > numImagesRequired)
                {
                    for (int index = 0; index < numGeneratedCells - numImagesRequired; index++)
                    {
                        DataGridViewCellStyle dataGridViewCellStyle = new DataGridViewCellStyle();
                        dataGridViewCellStyle.NullValue = null;
                        dataGridViewCellStyle.Tag = "BLANK";
                        dataViewImages.Rows[rowIndex].Cells[columnIndex + index].Style = dataGridViewCellStyle;
                    }
                }

                if (_currentStartImageIndex == 0)
                {
                    btnPreviousImages.Enabled = false;
                }
                else
                {
                    btnPreviousImages.Enabled = true;
                }

                if (_currentEndImageIndex < _files.Count)
                {
                    btnNextImages.Enabled = true;
                }
                else
                {
                    btnNextImages.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }


The following here is were after an image has been selected in the gridview will show in the pictureBox.
C#
private void dataViewImages_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
   Image image =(Image)dataViewImages.SelectedCells;
   pictureBox.Image = image;
   tsbSave.Enabled = true;
   tsbRotate.Enabled = true;
   tsbCCRotate.Enabled = true
}
Posted
Updated 13-Mar-10 22:23pm
v3

Your first step would be to post some code, your second would be to tell us exactly the error message. As this is winforms, not ASP.NET, I'd guess that your code can't create an image because the file is locked by the datagridview. The way around this bug in .NET is to load your file, clone it, and destroy/dispose the original, so the file is released.
 
Share this answer
 
this doesn't look good:
Image image =(Image)dataViewImages.SelectedCells


are you sure image is not null?
 
Share this answer
 
Thanks everyone but i managed to solve it :) thanks for the effort
 
Share this answer
 
Comments
Member 8395581 18-Nov-11 5:40am    
dszf
Member 8395581 18-Nov-11 5:40am    
Christian Graus505K
Have a Question or Comment?
Vote:


Solution 2
this doesn't look good:
Collapse | Copy Code
Image image =(Image)dataViewImages.SelectedCells

are you sure image is not null?
Hi

Could you please share the code. i want to view the images in picturebox after click / downarrow press in datagridview control. I have loaded the image names in datagridview, once i click the name image need to open.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900