1

I have a Windows console application in C# that acts as a phone book. I'm getting a Missing Directives error in the lines where the application is setting up the menu icons, such as this one:

cmsProgramMenu.Items.Add("&Settings", Properties.Resources.phone_receiver.ToBitmap(), OnSettings_Click);

The full error is 'object' does not contain a definition for 'ToBitmap' and no extension method 'ToBitmap' accepting a first argument of type 'object' could be found (are you missing a using directive or assembly reference?).

I've poked around online and on SO and all the directives that I've seen suggested are already included in my code, but the error persists. These are the ones that I have:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data;
using System.Windows.Media.Imaging;

Is there another one that I'm missing? Or is there another way of converting the images to Bitmap? I'm working in Visual Studio 2015.

AxxieD
  • 614
  • 1
  • 7
  • 28
  • I admit I don't fully understand the question, but is there any chance you just need a `()` at the end of `ToBitmap`? – adv12 Jun 23 '16 at 13:44
  • My apologies, that's already in the code but it appears I (somehow?) didn't copy it over. Adding parentheses doesn't fix the problem, but thank you for pointing that out. – AxxieD Jun 23 '16 at 13:46
  • 1
    Okay, since you say this is a Console app, is there any chance that you didn't add a reference to `System.Drawing.dll`? For a Windows Forms project, VS will automatically add this reference, but not for a Console project. – adv12 Jun 23 '16 at 13:48
  • 1
    Or do you need to first cast `Properties.Resources.phone_receiver` to an `Icon` before calling `ToBitmap()` on it? – adv12 Jun 23 '16 at 13:49

3 Answers3

2

The problem is that Properties.Resources.phone_receiver is of type Object and as the error message says Object doesn't have a method called ToBitmap. If you are sure that is is an Icon (which your code suggests it is) then you can do the following:

((Icon)Properties.Resources.phone_receiver).ToBitmap()

This explicitly casts Properties.Resources.phone_receiver to an Icon and then the compiler knows that it can call the ToBitmap method on it.

Chris
  • 27,210
  • 6
  • 71
  • 92
  • This is probably on the right track, but it's still flagging the same error. – AxxieD Jun 23 '16 at 14:33
  • @AxxieD, when you say "it's still flagging the same error," do you mean the original error (missing using directive/assembly reference) or the one you edited into your question (problem with `object.ToBitmap()`)? I think there are at least two things going on here... – adv12 Jun 23 '16 at 14:36
  • Can you confirm that it is the exact same error message on the exact same line? The line of code I've given you *cannot* have the error `'object' does not contain a definition for 'ToBitmap'...` because we are never trying to call that method on a variable of type `Object`. I'd believe that either the error message is different (ie can't cast) or that its the same error in a different place (because you do the same thing elsewhere)... – Chris Jun 23 '16 at 14:40
  • Also can you confirm what you actually expect to be in `Properties.Resources.phone_receiver`? It'd be useful to confirm it is an `Icon` and not a string pointing to a file (as some others have assumed). – Chris Jun 23 '16 at 14:42
  • 1
    Never mind, apparently it worked. It just took my compiler a few minutes to realize it. Thank you so much! – AxxieD Jun 23 '16 at 14:44
  • Update: I'm not getting another error when I try to run the app: `Unhandled exception of type 'System.NullReferenceException' occurred. Additional information: Object not set to an instance of an object.` Could this be related, or do you think it's something else entirely? – AxxieD Jun 23 '16 at 15:17
  • 1
    http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Chris Jun 23 '16 at 15:18
2

If you added the Icon as "Icon" in the resources window (project Properties/Resources), then it will already be typed as Icon. A reference to System.Drawing will be sufficient, and ToBitmap will appear in IntelliSense.

If, however you added the resource as "Image", then it will already be typed as Bitmap and ToBitmap() will not have to be called at all.

No casting of any kind should be necessary!

Resources selektion


If you hoover over "phone_receiver" in the code editor, a tooltip will appear and show you the type of the resource:

               hover the mouse here  |
                                     V

enter image description here

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Ah yes. Presumably if it is of type object then it is going to be there as type "other"? I've not used resources much so hadn't really thought about the fact that it could be strongly typed, i just went with fixing the error rather than potentially the root cause. – Chris Jun 23 '16 at 15:10
  • 1
    I tried adding an icon file as "other", but it still was typed as icon. Probably only files with extensions not known to the resource manager will be typed as `object`. – Olivier Jacot-Descombes Jun 23 '16 at 15:36
0

or try something like this if phone_receiver is an image

Bitmap myBitmap = new Bitmap(Properties.Resources.phone_receiver); cmsProgramMenu.Items.Add("&Settings", myBitmap, OnSettings_Click);