How can I include an image as a Resource in my project?

I want to add an image as a resource in my project so that I can reference it for programmatically inserting into a range in a spreadsheet.

I added the image by right-clicking the project and selecting Add > Existing Item...

I hoped that the image (.png file) would then be available using this code:

var logoRange = _xlSheet.Range[
                    _xlSheet.Cells[1, LOGO_FIRST_COLUMN], 
                    _xlSheet.Cells[5, LOGO_LAST_COLUMN]];
//System.Drawing.Bitmap logo = 
    //ReportRunner.Properties.Resources.pa_logo_notap.png;
System.Drawing.Image logo = 
    ReportRunner.Properties.Resources.pa_logo_notap.png;
_xlSheet.Paste(logoRange, logo);

...but using either Bitmap or Image, I get, "'ReportRunner.Properties.Resources' does not contain a definition for 'pa_logo_notap'"

This seemed like sensible code based on what I read here, but it seems that the image has to be explicitly marked as a resource for this to work. How do I accomplish that?

UPDATE

I tried this:

System.Drawing.Image logo = (System.Drawing.Image)ReportRunner.Properties.Resources.ResourceManager.GetObject("pa_logo_notag.png");
_xlSheet.Paste(logoRange, logo);

...but not only did I get a confirmation msg about the item being pasted not being the same size and shape as the place where it was being inserted, and did I really want to do that, it also inserted some seemingly unrelated text ("avgOrderAmountCell.Style") instead of the image.

UPDATE 2

Okay, I tried this:

Assembly myAssembly = Assembly.GetExecutingAssembly();
System.Drawing.Image logo = (System.Drawing.Image)myAssembly.GetName().Name + ".pa_logo_notap.png";
Clipboard.SetDataObject(logo, true);
_xlSheet.Paste(logoRange, logo);

...but get, "Cannot convert type 'string' to 'System.Drawing.Image' on the second line of that code.

UPDATE 3

This works:

private System.Drawing.Image _logo;
. . .
_logo = logo; // logo (the image) is passed in an overloaded constructor
. . .
var logoRange = _xlSheet.Range[
    _xlSheet.Cells[1, LOGO_FIRST_COLUMN], _xlSheet.Cells[6, 
    LOGO_LAST_COLUMN]];
Clipboard.SetDataObject(_logo, true);
_xlSheet.Paste(logoRange, _logo);

...but I'm not crazy about it, because I'm using an image that is on a form, and passing the image to this class's constructor. Passing images around seems kind of goofy when it should be possible to store the image as a resource and just load the resource. I still haven't gotten that methodology to work, though...

UPDATE 4

I reckon I'll just stick with what I've got (in Update 3), kludgy as it is, because this:

Assembly myAssembly = Assembly.GetExecutingAssembly();
Stream myStream = 
    myAssembly.GetManifestResourceStream(myAssembly.GetName().Name + 
    "pa_logo_notap.png");
Bitmap bmp = new Bitmap(myStream);
Clipboard.SetDataObject(bmp, true);
_xlSheet.Paste(logoRange, bmp);

...fails with, "Value of 'null' is not valid for 'stream'"

Answers


You have change the build action of the image to be embedded resource.

Then you can reference by doing:

UPDATED

 Assembly myAssembly = Assembly.GetExecutingAssembly();
 Stream myStream = myAssembly.GetManifestResourceStream( myAssembly.GetName().Name + ".images.pa_logo_notap.png");
  Bitmap bmp = new Bitmap(myStream);


Need Your Help

Configure Memcached in Laravel

laravel laravel-5 memcached laravel-5.1

I've tried to set up memcached with my Laravel

seam-gen, netbeans and completion

java netbeans seam facelets netbeans6.5

How can I make seam-gen work with NetBeans 6.5 so that completion of tags (s:, ui:, rich:, ...) works? In the free-form project that seam-gen creates, this does not seam to work. If I create a stan...