Draw icon in PictureBox

K

Kent

I'm stuck on something that I'm sure is simple but I can't find an easy
answer. I want a set of clickable icons on a form. I figured using a
PictureBox would be the easiest method but getting the image loaded from an
icon file is a problem.

With VS2003 I was able to create a Graphics object in the PictureBox paint
event and use DrawIcon to display the icon. I figured there must be a
better way to do this but never found it (since the Icon.ToBitmap() method
isn't supported in CF). Anyway, in VS2005 I get this warning:

"Members not supported by the device platform should not be called:
System.Windows.Forms.PictureBox.add_Paint is not a supported method in this
Platform."

Two questions:

1. Is there a better way to load my icon from a resource into a PictureBox?
2. Is the paint event really no longer supported for the PictureBox in CF
(if it ever was)?

Thanks.
 
T

tamberg

1) Maybe it's easier to create your own Control using

e.Graphics.DrawIcon(i, x, y);

given OnPaint or the Paint event's PaintEventArgs e.
 
K

Kent

Thanks for the suggestion. I haven't created a custom Control before but I
guess that is a possibility if the CF PictureBox won't do what I need. It
just seems like this is such a basic thing that I assumed there was a way to
do it with the existing control.

Unless I hear a better suggestion then I guess I'll look at doing it that
way. Thanks again.
 
S

Sergey Bogdanov

Well, you have at least several solutions here. The first one is to
create a new class (say PictureBoxEx) and derive it from PictureBox
class. Then you can override OnPaint method and draw icon there:

public class PictureBoxEx : PictureBox
{
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);

Icon icon = new
Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream("[ApplicationNamespace].Icon1.ico"));
e.Graphics.DrawIcon(icon, 0, 0);
icon.Dispose();
}
}

When you compile it, PictureBoxEx will appear in the Toolbox - just
drag&drop it to your form.

The second solution - you can replace PictureBox control with a Panel
control which has the Paint event that you are looking for.
 
K

Kent

I've followed your first suggestion and that works for me. Thanks.

Sergey Bogdanov said:
Well, you have at least several solutions here. The first one is to create
a new class (say PictureBoxEx) and derive it from PictureBox class. Then
you can override OnPaint method and draw icon there:

public class PictureBoxEx : PictureBox
{
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);

Icon icon = new
Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream("[ApplicationNamespace].Icon1.ico"));
e.Graphics.DrawIcon(icon, 0, 0);
icon.Dispose();
}
}

When you compile it, PictureBoxEx will appear in the Toolbox - just
drag&drop it to your form.

The second solution - you can replace PictureBox control with a Panel
control which has the Paint event that you are looking for.

--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com

I'm stuck on something that I'm sure is simple but I can't find an easy
answer. I want a set of clickable icons on a form. I figured using a
PictureBox would be the easiest method but getting the image loaded from
an icon file is a problem.

With VS2003 I was able to create a Graphics object in the PictureBox
paint event and use DrawIcon to display the icon. I figured there must
be a better way to do this but never found it (since the Icon.ToBitmap()
method isn't supported in CF). Anyway, in VS2005 I get this warning:

"Members not supported by the device platform should not be called:
System.Windows.Forms.PictureBox.add_Paint is not a supported method in
this Platform."

Two questions:

1. Is there a better way to load my icon from a resource into a
PictureBox?
2. Is the paint event really no longer supported for the PictureBox in
CF (if it ever was)?

Thanks.
 

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