Add image to Context Menu Strip Menu Item in code

J

Joe Cool

I am trying to add a project resource image to a contextmenustrip
menuitem in code. Here is the code:

ContextMenuStrip cm = new ContextMenuStrip();
Image image;
Stream s;

s = this.GetType().Assembly.GetManifestResourceStream
("myProjectName.resourcepicturename");
image = Image.FromStream(s);
cm.Items.Add(new ToolStripMenuItem("Menu Item Text",
image, this.MenuItemHandler_Click));

It dies trying to assign the image from the stream to the Image
variable. Error is:

Value of 'null' is not valid for 'stream'.

Any help appreciated.
 
J

Jeff Johnson

I am trying to add a project resource image to a contextmenustrip
menuitem in code. Here is the code:

ContextMenuStrip cm = new ContextMenuStrip();
Image image;
Stream s;

s = this.GetType().Assembly.GetManifestResourceStream
("myProjectName.resourcepicturename");
image = Image.FromStream(s);
cm.Items.Add(new ToolStripMenuItem("Menu Item Text",
image, this.MenuItemHandler_Click));

It dies trying to assign the image from the stream to the Image
variable. Error is:

Value of 'null' is not valid for 'stream'.

Any help appreciated.

Are you sure you have the case correct in
"myProjectName.resourcepicturename"? And how did you determine you should be
using <projectName>.<resourceName> in the first place? Have you done this
sort of thing before?
 
J

Joe Cool

Are you sure you have the case correct in
"myProjectName.resourcepicturename"? And how did you determine you shouldbe
using <projectName>.<resourceName> in the first place? Have you done this
sort of thing before?

I found this page:

http://support.microsoft.com/kb/324567

But it doesn't spefically describe what I want to do. The image
property for a ContextMenuStrip ToolStripMenuItem is an Image
datatype. The resource image was loaded from a JPG file. I gathered
from that webpage, the resource name was case-sensitive.

Knowing that an Image datatype variable can be loaded with the
Image.FromStream method, I decided to try this approach, but I don't
know what I am doing wrong.
 

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