在C#中从DataGridView到PictureBox的图像 [英] Image from DataGridView to PictureBox in C#

查看:65
本文介绍了在C#中从DataGridView到PictureBox的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我无法在图片框中显示图像.我打开一个文件,并用文件夹中的图像填充我的datagridview.我希望每次用户从gridview中选择图像时,图像都将显示在图片框中.我尝试了不同的方法,但出现错误,无法更改为图像.任何帮助都会很棒.

以下是用于将图像加载到gridview中的代码:

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);
            }
        }



以下是在gridview中选择图像后将显示在pictureBox中的内容.



The following here is were after an image has been selected in the gridview will show in the pictureBox.

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
}

推荐答案

您的第一步将是发布一些代码,而您的第二步将是告诉我们确切的错误消息.由于这是winforms,而不是ASP.NET,因此我猜您的代码无法创建图像,因为该文件已由datagridview锁定.解决.NET中此错误的方法是加载文件,克隆文件并销毁/处置原始文件,以便释放文件.
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.


这看起来不太好:
this doesn''t look good:
Image image =(Image)dataViewImages.SelectedCells



您确定图像不为空吗?



are you sure image is not null?


谢谢大家,但我设法解决了这个问题:)感谢您的努力
Thanks everyone but i managed to solve it :) thanks for the effort


这篇关于在C#中从DataGridView到PictureBox的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆