473,480 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do you draw an icon in grayscale?

I've written a class that inherits from MenuItem which displays icons
next to the items in a menu... The only problem I'm having is that I
have no clue how to turn these icons greyscale if they're item is
disabled. Right now I'm turning the text grey and would like to also
grey out the icon. Any help would be awesome. here's my code for
drawing the icon:
if ( this._icon != null )
{
if ( this.Text != "" && this.Text != "X" )
e.Graphics.DrawIcon(this._icon, e.Bounds.Left+ICON_PAD_LEFT,
e.Bounds.Top+ICON_PAD_TOP);
else
e.Graphics.DrawIcon(this._icon, e.Bounds.Left + ((e.Bounds.Width -
this._icon.Width) / 2), e.Bounds.Top+ICON_PAD_TOP); // center the icon
}
Nov 17 '05 #1
3 6454
I found the ControlPaint class... however i'm now wondering:
Would this be bad? I'm not sure if it's going to leak memory or not -
can I get some feedback? thanks :)

if (this._icon != null)
{
if (this.Text != "" && this.Text != "X")
{
if (this.Enabled)
e.Graphics.DrawIcon(this._icon, e.Bounds.Left+ICON_PAD_LEFT,
e.Bounds.Top+ICON_PAD_TOP);
else
ControlPaint.DrawImageDisabled(e.Graphics, this._icon.ToBitmap(),
e.Bounds.Left+ICON_PAD_LEFT, e.Bounds.Top+ICON_PAD_TOP, br.Color);
}
else
{
// center icon:
if (this.Enabled)
e.Graphics.DrawIcon(this._icon, e.Bounds.Left +
((e.Bounds.Width - this._icon.Width) / 2), e.Bounds.Top+ICON_PAD_TOP);
else
ControlPaint.DrawImageDisabled(e.Graphics, this._icon.ToBitmap(),
e.Bounds.Left + ((e.Bounds.Width - this._icon.Width) / 2),
e.Bounds.Top+ICON_PAD_TOP, br.Color);
}
}

Benny Raymond wrote:
I've written a class that inherits from MenuItem which displays icons
next to the items in a menu... The only problem I'm having is that I
have no clue how to turn these icons greyscale if they're item is
disabled. Right now I'm turning the text grey and would like to also
grey out the icon. Any help would be awesome. here's my code for
drawing the icon:
if ( this._icon != null )
{
if ( this.Text != "" && this.Text != "X" )
e.Graphics.DrawIcon(this._icon, e.Bounds.Left+ICON_PAD_LEFT,
e.Bounds.Top+ICON_PAD_TOP);
else
e.Graphics.DrawIcon(this._icon, e.Bounds.Left + ((e.Bounds.Width
- this._icon.Width) / 2), e.Bounds.Top+ICON_PAD_TOP); // center the icon
}

Nov 17 '05 #2
I don't see anything wrong with your solution. GC will take care of the
bitmap. If you want to be really belt-and-braces you could do something
like:

Bitmap bm=icon.tobitmap;
ControlPaint.DrawImageDisabled(...);
bm.Dispose();

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Benny Raymond" <be***@pocketrocks.com> wrote in message
news:Oo*************@tk2msftngp13.phx.gbl...
I found the ControlPaint class... however i'm now wondering:
Would this be bad? I'm not sure if it's going to leak memory or not - can
I get some feedback? thanks :)

if (this._icon != null)
{
if (this.Text != "" && this.Text != "X")
{
if (this.Enabled)
e.Graphics.DrawIcon(this._icon, e.Bounds.Left+ICON_PAD_LEFT,
e.Bounds.Top+ICON_PAD_TOP);
else
ControlPaint.DrawImageDisabled(e.Graphics, this._icon.ToBitmap(),
e.Bounds.Left+ICON_PAD_LEFT, e.Bounds.Top+ICON_PAD_TOP, br.Color);
}
else
{
// center icon:
if (this.Enabled)
e.Graphics.DrawIcon(this._icon, e.Bounds.Left +
((e.Bounds.Width - this._icon.Width) / 2), e.Bounds.Top+ICON_PAD_TOP);
else
ControlPaint.DrawImageDisabled(e.Graphics, this._icon.ToBitmap(),
e.Bounds.Left + ((e.Bounds.Width - this._icon.Width) / 2),
e.Bounds.Top+ICON_PAD_TOP, br.Color);
}
}

Benny Raymond wrote:
I've written a class that inherits from MenuItem which displays icons
next to the items in a menu... The only problem I'm having is that I have
no clue how to turn these icons greyscale if they're item is disabled.
Right now I'm turning the text grey and would like to also grey out the
icon. Any help would be awesome. here's my code for drawing the icon:
if ( this._icon != null )
{
if ( this.Text != "" && this.Text != "X" )
e.Graphics.DrawIcon(this._icon, e.Bounds.Left+ICON_PAD_LEFT,
e.Bounds.Top+ICON_PAD_TOP);
else
e.Graphics.DrawIcon(this._icon, e.Bounds.Left +
((e.Bounds.Width - this._icon.Width) / 2), e.Bounds.Top+ICON_PAD_TOP); //
center the icon
}

Nov 17 '05 #3
Awesome, thanks...

Lately I've been realy cautious because back when I didn't understand
drawing quite as much, I had a function that was leaking about 20mb a
second... ;)
Bob Powell [MVP] wrote:
I don't see anything wrong with your solution. GC will take care of the
bitmap. If you want to be really belt-and-braces you could do something
like:

Bitmap bm=icon.tobitmap;
ControlPaint.DrawImageDisabled(...);
bm.Dispose();

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3968
by: tjh | last post by:
hi - i'm a bit new to PHP - i've been messing around with the GD2 image functions and was wondering: i see a lot of information about converting a color image to grayscale but i can't figure...
8
11372
by: RicercatoreSbadato | last post by:
I'm using bmp.Save() and the bmp is in PixelFormat.Format8bppIndexed. But when I open the image with Gimp, it tells me that the image is RGB and not grayscale.
0
2118
by: tlemcenvisit | last post by:
Hello I translated this code (witch convert an image to grayscale) from C#.NET to C++.NET The C#.NET code is : private void GrayScale(Bitmap b) { BitmapData bmData = b.LockBits(new...
2
3805
by: Harry Simpson | last post by:
I'm having trouble reducing the size of a bitmap programmatically: The file size of the original is 2400k. The picture is 816x1056. 16 million colors i think. I only need grayscale render. ...
2
3239
by: Henry Wu | last post by:
Hi I was at the search for making e.Graphics.DrawImage turn any image to Grayscale, and I found two similar solutions but different ColorMatrix values, what is the difference between the two? Is...
0
962
by: pope | last post by:
I am up for shade icon when I click the icon which I draw on a SDI viw with CImageList::Draw(pDC,index,myPoint, ILD_BLEND50). the icons are edited in the vc++ icon editor, with the 'transparent'...
0
895
by: pope | last post by:
I use a CImageList to draw icons on the SDI view: ... myImageList.Create(48,48, ILC_COLOR16,1,1); ... But the icon drew on the SDI view is of black background,why? the icon is of greenish...
8
47830
by: platinumhimani | last post by:
-How to convert any image(8,16,24,32 or 64-bit) to 8-bit grayscale -i have tried to convert a 24-bit image to grayscale using setpixel and getpixel functions, in vb.net but i am unable to save...
6
3754
by: ProtossLee | last post by:
Hi, I am currently working on a project for image processing. a double matrix m1(1300X1000) need to be converted into bitmap and displayed on screen. so far I've made the following code: For i...
0
6918
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7057
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7102
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6756
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4798
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4495
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3008
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
570
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.