Grey images in TreeView

  • Thread starter Fabio Cannizzo via .NET 247
  • Start date
F

Fabio Cannizzo via .NET 247

The TreeNode of my TreeView have a property that identify if the node as active or inactive.

I would like the Image associated with the particular inactive nodes to appear greyed.

My idea was to load two copies of each image in the ImageList, one normal and one greyed, but I do not know how to create a greyed bitmap from a normal bitmap.

Can anybody help?

Thanks,
Fabio
 
F

Frank Eller

Hi Fabio,
I do not know how to create a greyed
bitmap from a normal bitmap.

Use:
Photoshop
Corel Photopaint
PaintShop Pro
....
any Graphics program can do it.

You can of course also use .NET :

using System;
using System.Drawing;
using System.Drawing.Imaging;

....

public static Bitmap CreateGrayscaledBitmap( Image image )
{
// Create new Bitmap with size of the original bitmap
Bitmap bitmap = new Bitmap( image.Width, image.Height );

// create a matrix to convert the colors
ColorMatrix colorMatrix = new ColorMatrix( new float[][] {
new float[] {0.3F, 0.3F, 0.3F, 0, 0},
new float[] {0.59F, 0.59F, 0.59F, 0, 0},
new float[] {0.11F, 0.11F, 0.11F, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
} );

ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.SetColorMatrix( colorMatrix );
Graphics g = Graphics.FromImage( bitmap );
g.DrawImage( image, new Rectangle( 0, 0, image.Width, image.Height ),
0, 0, image.Width, image.Height, GraphicsUnit.Pixel,
imageAttributes );
g.Dispose();

return bitmap;
}

Regards,

Frank Eller
www.frankeller.de
 
T

Tim Wilson

As an alternative to using a ColorMatrix, you could also use the static
DrawImageDisabled method of the ControlPaint class.

--
Tim Wilson
..Net Compact Framework MVP

Frank Eller said:
Hi Fabio,
I do not know how to create a greyed
bitmap from a normal bitmap.

Use:
Photoshop
Corel Photopaint
PaintShop Pro
...
any Graphics program can do it.

You can of course also use .NET :

using System;
using System.Drawing;
using System.Drawing.Imaging;

...

public static Bitmap CreateGrayscaledBitmap( Image image )
{
// Create new Bitmap with size of the original bitmap
Bitmap bitmap = new Bitmap( image.Width, image.Height );

// create a matrix to convert the colors
ColorMatrix colorMatrix = new ColorMatrix( new float[][] {
new float[] {0.3F, 0.3F, 0.3F, 0, 0},
new float[] {0.59F, 0.59F, 0.59F, 0, 0},
new float[] {0.11F, 0.11F, 0.11F, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
} );

ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.SetColorMatrix( colorMatrix );
Graphics g = Graphics.FromImage( bitmap );
g.DrawImage( image, new Rectangle( 0, 0, image.Width, image.Height ),
0, 0, image.Width, image.Height, GraphicsUnit.Pixel,
imageAttributes );
g.Dispose();

return bitmap;
}

Regards,

Frank Eller
www.frankeller.de
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top