.ToBitmap:缺少 'Using Directive' 或 'Assembly Reference'
.ToBitmap: Missing 'Using Directive' or 'Assembly Reference'
我有一个用 C# 编写的 Windows 控制台应用程序,它充当 phone 一本书。我在应用程序设置菜单图标的行中收到一个 Missing Directives 错误,例如这个:
cmsProgramMenu.Items.Add("&Settings", Properties.Resources.phone_receiver.ToBitmap(), OnSettings_Click);
完整的错误是'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?)
。
我在网上和 SO 上四处寻找,我看到的所有建议指令都已包含在我的代码中,但错误仍然存在。这些是我拥有的:
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;
还有我遗漏的吗?还是有另一种将图像转换为位图的方法?我在 Visual Studio 2015 年工作。
如果 phone_receiver 是一张图片,请尝试这样的操作
位图 myBitmap = 新位图(Properties.Resources.phone_receiver);
cmsProgramMenu.Items.Add("&设置", myBitmap, OnSettings_Click);
问题是 Properties.Resources.phone_receiver
是 Object
类型,并且正如错误消息所说 Object
没有名为 ToBitmap
的方法。如果您确定这是一个 Icon
(您的代码表明它是),那么您可以执行以下操作:
((Icon)Properties.Resources.phone_receiver).ToBitmap()
这显式地将 Properties.Resources.phone_receiver
转换为 Icon
,然后编译器知道它可以调用 ToBitmap
方法。
如果您在资源 window(项目 Properties/Resources)中将图标添加为 "Icon",那么它已经被输入为 Icon
。对 System.Drawing
的引用就足够了,ToBitmap
将出现在 IntelliSense 中。
但是,如果您将资源添加为 "Image",那么它已经被键入为 Bitmap
,并且根本不需要调用 ToBitmap()
。
不需要任何类型的转换!
如果您在代码编辑器中将鼠标悬停在 "phone_receiver" 上,将出现一个工具提示并显示资源类型:
hover the mouse here |
V
我有一个用 C# 编写的 Windows 控制台应用程序,它充当 phone 一本书。我在应用程序设置菜单图标的行中收到一个 Missing Directives 错误,例如这个:
cmsProgramMenu.Items.Add("&Settings", Properties.Resources.phone_receiver.ToBitmap(), OnSettings_Click);
完整的错误是'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?)
。
我在网上和 SO 上四处寻找,我看到的所有建议指令都已包含在我的代码中,但错误仍然存在。这些是我拥有的:
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;
还有我遗漏的吗?还是有另一种将图像转换为位图的方法?我在 Visual Studio 2015 年工作。
如果 phone_receiver 是一张图片,请尝试这样的操作
位图 myBitmap = 新位图(Properties.Resources.phone_receiver); cmsProgramMenu.Items.Add("&设置", myBitmap, OnSettings_Click);
问题是 Properties.Resources.phone_receiver
是 Object
类型,并且正如错误消息所说 Object
没有名为 ToBitmap
的方法。如果您确定这是一个 Icon
(您的代码表明它是),那么您可以执行以下操作:
((Icon)Properties.Resources.phone_receiver).ToBitmap()
这显式地将 Properties.Resources.phone_receiver
转换为 Icon
,然后编译器知道它可以调用 ToBitmap
方法。
如果您在资源 window(项目 Properties/Resources)中将图标添加为 "Icon",那么它已经被输入为 Icon
。对 System.Drawing
的引用就足够了,ToBitmap
将出现在 IntelliSense 中。
但是,如果您将资源添加为 "Image",那么它已经被键入为 Bitmap
,并且根本不需要调用 ToBitmap()
。
不需要任何类型的转换!
如果您在代码编辑器中将鼠标悬停在 "phone_receiver" 上,将出现一个工具提示并显示资源类型:
hover the mouse here | V