65.9K
CodeProject 正在变化。 阅读更多。
Home

在 DataGridView 单元格中显示/隐藏图像

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.56/5 (8投票s)

2011年 5月 27日

CPOL
viewsIcon

47559

downloadIcon

2279

本文讨论了如何在 DataGridView 单元格中显示/隐藏图像。

引言

ShowHideImage/Image_1.png

图 1

本文介绍如何在 DataGridViewCell 中显示和隐藏图像。用户还可以选择在可用显示区域内查看图像。

ShowHideImage/Image_1.png

图 2

以下例程可帮助在单元格中显示图像。单元格中的图像保存为字节数组,然后将其转换回图像,再用图像加载的 DataGridViewImageCell 替换 DataGridViewTextboxCell

/// <summary>
/// Routine Perform ShowPicture for the
/// selected cell in the provided GridControl
/// </summary>
/// <param name="myGrid"></param>
/// <param name="dgvCell"></param>
/// <param name="fillIn"></param>
private void ShowPicture(DataGridView myGrid, DataGridViewCell dgvCell, bool fillIn)
{
    if (!_myAttachments.ContainsKey(dgvCell.RowIndex) || 
		_myAttachments[dgvCell.RowIndex] == null)
        return;

    //get the image
    byte[] byteData = _myAttachments[dgvCell.RowIndex];
    Image returnImage = null;

    //I was not able to figure out a way to check the format of bytearray, instead     
    //I'm using a try-catch statement which will throw an exception if not an image
    try
    {
        //Convert byte to Image
        MemoryStream ms = new MemoryStream(byteData);
        returnImage = Image.FromStream(ms, false, true);
    }
    catch
    {
        MessageBox.Show("Not an Image File", "Show Picture", 
		MessageBoxButtons.OK, MessageBoxIcon.Warning);
        return;
    }

    //Initialize new DataGridViewImageCell for the image to load
    DataGridViewImageCell dgImageCol = new DataGridViewImageCell();

    //Resize the Image
    if (fillIn)
        returnImage =ResizeImageToFitInsideTheGrid(returnImage, myGrid);

    dgImageCol.Value = returnImage;

    //When ImageWidth is greater than ColumnWidth
    //Increase the width of the column to show full size of the image
    if (myGrid.Columns[dgvCell.ColumnIndex].Width < returnImage.Width)
        myGrid.Columns[dgvCell.ColumnIndex].Width = returnImage.Width;

    //Increase the height of the column to show full size of the image   
    if (myGrid.Rows[dgvCell.RowIndex].Height < returnImage.Height)
        myGrid.Rows[dgvCell.RowIndex].Height = returnImage.Height;

    //Assign the DataGridViewImageCell to the Selected Cell
    myGrid[dgvCell.ColumnIndex, dgvCell.RowIndex] = dgImageCol;
}

隐藏图像例程,用 DataGridViewTextboxCell 替换 DataGridViewImageCell,并在其中加载文件名。

private void HidePicture(DataGridView myGrid, DataGridViewCell dgvCell)
{
    Type tp = dgvCell.GetType();
    //Check the cell type of DatabaseStorage Field is "DataGridViewTextBoxCell"
    if (tp.Name == "DataGridViewImageCell")
    {
        //Initialize new DataGridViewTextBoxCell for the text to load
        DataGridViewTextBoxCell dgTxtCol = new DataGridViewTextBoxCell();
        //Assign the file name to the cell
        dgTxtCol.Value = _myAttachmentFileNames[dgvCell.RowIndex];

        //Assign the DataGridViewTextBoxCell to the Cell of DatabaseStorage Field 
        myGrid[dgvCell.ColumnIndex, dgvCell.RowIndex] = dgTxtCol;
        myGrid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
    }
}

调整图像大小

  1. 宽度和高度都较大的图像
  2. 宽度较大的图像
  3. 高度较大的图像
  4. 宽度较小的图像
private Image ResizeImageToFitInsideTheGrid(Image returnImage, DataGridView myGrid)
{
    //Case 1: Image with Large Width & Height - 
    //      Reduce image height to display height & proportionate width
    //      if the proportionate width is still greater than the display width
    //      Reduce image width to display width & proportionate height
    //Case 2: Image with Large Width - 
    //      Reduce image width to display Width & proportionate Height
    //Case 3: Image with Large Height - 
    //      Reduce image height to display Height + proportionate Width
    //Case 4: Image with Small Width & Height - Do Nothing

    //Multiplying by 0.95 helps the image to fit inside the display area

    int dgvDisplayHeight = Convert.ToInt32
	((myGrid.DisplayRectangle.Height - myGrid.ColumnHeadersHeight) * .95);
    int dgvDisplayWidth = Convert.ToInt32
	((myGrid.DisplayRectangle.Width - myGrid.RowHeadersWidth) * .95);

    //Large Width & Large Height 
    if (returnImage.Width >= dgvDisplayWidth && returnImage.Height >= dgvDisplayHeight)
    {
        int h = Convert.ToInt32(dgvDisplayHeight);
        int w = Convert.ToInt32(h * (Convert.ToDouble(returnImage.Width) / 
			Convert.ToDouble(returnImage.Height)));
        //if the proportionate width is still greater than the display width
        if (w > dgvDisplayWidth)
        {
            w = Convert.ToInt32(dgvDisplayWidth);
            h = Convert.ToInt32(w * (Convert.ToDouble(returnImage.Height) / 
			Convert.ToDouble(returnImage.Width)));
        }
        returnImage = ResizeImage(returnImage, w, h);
    }
    //Large Height 
    else if (returnImage.Width <= dgvDisplayWidth && returnImage.Height >= 
			dgvDisplayHeight)
    {
        int h = Convert.ToInt32(dgvDisplayHeight);
        int w = Convert.ToInt32(h * (Convert.ToDouble(returnImage.Width) / 
			Convert.ToDouble(returnImage.Height)));
        returnImage = ResizeImage(returnImage, w, h);
    }
    //Large Width
    else if (returnImage.Width >= dgvDisplayWidth && 
		returnImage.Height <= dgvDisplayHeight)
    {
        int w = Convert.ToInt32(dgvDisplayWidth);
        int h = Convert.ToInt32(w * 
	(Convert.ToDouble(returnImage.Height) / Convert.ToDouble(returnImage.Width)));
        returnImage = ResizeImage(returnImage, w, h);
    }
    return returnImage;
}

/// <summary>
/// Routine will resize the given images with
/// the provided height and width
/// </summary>
/// <param name="actualImage">Original Image</param>
/// <param name="width">new width</param>
/// <param name="height">new height</param>
/// <returns>Resized Image</returns>
private Image ResizeImage(Image actualImage, int width, int height)
{
    Bitmap bitmap = new Bitmap(width, height);
    using (Graphics graphics = Graphics.FromImage((Image)bitmap))
        graphics.DrawImage(actualImage, 0, 0, width, height);

    return (Image)bitmap;
}
© . All rights reserved.