Icon is not a control, so you cannot add it to the panel the same way you would add a control. You can only draw an icon in any control's client area. Please see:
http://msdn.microsoft.com/en-us/library/system.drawing.icon.aspx[
^].
Perhaps the closest to your requirements solution would be this: create some custom control which renders some icon in it. Insert instanced of such control in your group box. To draw something, including an icon, on a control, you need to handle its event
Paint
, or, better yet, override its virtual method
OnPaint
. In either way, do not create an instance of
System.Drawing.Graphics
(which is a very common mistake), but use the instance passed to you as an event arguments parameter of your event handler of overridden method. Use the method
System.Drawing.Graphics.DrawIcon
:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawicon.aspx[
^].
However, I don't see cases where drawing icons in the client area would be useful. Icons carry specific functionality (multiple images with different size and color spaces) which are required to makes them application icons and windows icons, to be properly treated by the Windows UI, such as Shell. In all other cases you really need to use
Bitmap
or other
Image
and
System.Drawing.Graphics.DrawImage
:
http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage.aspx[
^].
As this case with using image is very simple, you can use the ready-to-use control specially designed for showing images in simple cases. This is
PictureBox
:
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.aspx[
^].
Please remember that this simple class is often abused. It is only good if you need something really simple (such as in your case). I explained it in my past answers:
How do I clear a panel from old drawing[
^],
draw a rectangle in C#[
^],
Append a picture within picturebox[
^].
—SA