Skip to main content Skip to footer

C1PictureBox : Displaying Disabled Images

The C1PictureBox control is a data bound control which shows picture images stored in a data source field, derived from System.Windows.Forms.PictureBox. Though it has 'Enabled' property' setting this property to False does not have any effect on the image being displayed. With this blog let's discuss not one but two approaches to handle this.

Approach 1 : Using Paint Event

The first approach is to handle the Paint event of the C1PictureBox .If the control is disabled, then repaint the image with a little grey so that it looks disabled. The code looks as follows :

//First Approach - using Paint event  
private void c1PictureBox1_Paint(object sender, PaintEventArgs e)  
{  
    if (!c1PictureBox1.Enabled)  
    {  
        using (Brush fBrush = new SolidBrush(Color.FromArgb(171, 71, 71, 71)))  
            e.Graphics.FillRectangle(fBrush, this.c1PictureBox1.ClientRectangle);  
    }  
}

Approach 2 : Using 2 Images

The second approach is to use two image objects - one for the default image and the other which is a greyed version of the image. To generate two images, the following code can be used :

Bitmap enabledImage;  
Bitmap disabledImage;  
//Create two image objects  
void GenerateImages()  
{  
    enabledImage = new Bitmap(c1PictureBox2.Image);  
    disabledImage = new Bitmap(enabledImage.Width, enabledImage.Height);  

    using (Graphics G = Graphics.FromImage(disabledImage))  
    {  
        using (ImageAttributes IA = new ImageAttributes())  
        {  
            ColorMatrix CM = new ColorMatrix();  
            CM.Matrix33 = .5f;  
            IA.SetColorMatrix(CM);  
            Rectangle R = new Rectangle(0, 0, enabledImage.Width, enabledImage.Height);  
            G.DrawImage(enabledImage, R, R.X, R.Y, R.Width, R.Height, GraphicsUnit.Pixel, IA);  
        }  
    }  
}

Once the images are generated, simply use the EnabledChanged event of the C1PictureBox to set the C1PictureBox.Image as per the Enabled property.

//Second Approach - using two Image objects  
private void c1PictureBox2_EnabledChanged(object sender, EventArgs e)  
{  
    c1PictureBox2.Image = c1PictureBox2.Enabled ? enabledImage : disabledImage;  
}

Please refer to the attached samples for the implementation. Download Sample (CS) Download Sample (VB)

MESCIUS inc.

comments powered by Disqus