Click here to Skip to main content
11,642,855 members (70,646 online)
Click here to Skip to main content

Tagged as

Show Hide Image in DataGridView Cell

, 27 May 2011 CPOL 22.8K 1.8K 14
Rate this:
Please Sign up or sign in to vote.
This post discusses how to show hide image in DataGridView cell

Introduction

Image_1.png

Image 1

This post is regarding how to show and hide Image within the DataGridViewCells. The user can also opt to view the image within the available display area.

Image_1.png

Image 2

The following routine helps to show the image in the cell. The Image in the cell is saved as bytearray which is converted back to image, then replaces the DataGridViewTextboxCell with an DataGridViewImageCell where the image cell is loaded with the image.

/// <span class="code-SummaryComment"><summary>
</span>

Hide the Picture routine, replace the DataGridViewImageCell with a DataGridViewTextboxCell and load the file name in it.

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

Resizing the Image

  1. Image with large Width & Height
  2. Image with large Width
  3. Image with large Height
  4. Image with small Width
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;
}

/// <span class="code-SummaryComment"><summary>
</span>

License

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

Share

About the Author

Balu Sathish
Software Developer Calpine Technologies
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionAdding image to a SQLite database. Pin
CodeWarrior22-Dec-14 15:25
memberCodeWarrior22-Dec-14 15:25 
GeneralMy vote of 5 Pin
Mazen el Senih29-Mar-13 5:32
memberMazen el Senih29-Mar-13 5:32 
QuestionQestion Pin
suraj kumar sah17-Mar-12 23:48
membersuraj kumar sah17-Mar-12 23:48 
AnswerMy vote of 5 Pin
Luc Pattyn27-May-11 4:34
mvpLuc Pattyn27-May-11 4:34 
GeneralRe: My vote of 5 Pin
balu.s29-May-11 19:42
memberbalu.s29-May-11 19:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 27 May 2011
Article Copyright 2011 by Balu Sathish
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid