AxxieD AxxieD - 2 months ago 9x
C# Question

.ToBitmap: Missing 'Using Directive' or 'Assembly Reference'

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.


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:


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