看似简单的 .NET 导入
我需要调用这个方法,但在任何命名空间中都找不到它。
http://msdn.microsoft.com/ en-us/library/system.windows.forms.control.creategraphics.aspx
它说它位于 System.Windows.Forms 命名空间和 System.Windows.Forms.dll 中。
我正在使用 Visual Studio 2010 并将 System.Windows.Forms 引用导入到项目中。
在顶部,我进行了导入(sry,我的 Java 俚语)using FORMS = System.Windows.Forms;
Graphics g = FORMS.CreateGraphics(); // error
错误是:“命名空间“System”中不存在类型或命名空间名称“CreateGraphics” .Windows.Forms(您是否缺少程序集参考?)”
感谢您的帮助。
---编辑---
我正在尝试运行这篇文章的最佳答案中的代码:
---编辑---
根据用户的建议,我正在尝试:
Graphics g = new Graphics();
FORMS.Control a = new FORMS.Control();
g = a.CreateGraphics();
- -编辑---
没关系,抱歉人们,我很愚蠢。稍后会接受。
I need to call this method, but I can't find it in any namespace.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.creategraphics.aspx
It says it is in System.Windows.Forms namespace and in System.Windows.Forms.dll.
I am using Visual Studio 2010 and imported the System.Windows.Forms reference into the project.
At the top I made an import (sry, my Java slang) using FORMS = System.Windows.Forms;
Graphics g = FORMS.CreateGraphics(); // error
Error is: "The type or namespace name 'CreateGraphics' does not exist in the namespace 'System.Windows.Forms (are you missing an assembly reference?)"
Thanks for any help.
---EDIT---
I am trying to run the code from the top answer from this post:
GDI+ / C#: How to save an image as EMF?
---EDIT---
With a user's suggestions, I am trying:
Graphics g = new Graphics();
FORMS.Control a = new FORMS.Control();
g = a.CreateGraphics();
---EDIT---
Never mind, sorry people, I am stupid. Will accept in a second.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这
是一个命名空间别名,而不是类型别名。
命名空间没有定义方法。
此外,
CreateGraphics
不是静态的,因此也不能直接在类型上调用,只能在实例上调用。假设
myForm
是Form
:在链接的示例中,
CreateGraphics()
在this
实例上隐式调用,并假设该调用在表单
或控制
类型。因此,如果您在控件/表单中调用它,它就会起作用。This:
Is a namespace alias, not a type alias.
Namespaces do not have methods defined on them.
Additionally,
CreateGraphics
is not static, so can't be called directly on a type either, only on an instance.Assuming that
myForm
is an instance ofForm
:In your linked example,
CreateGraphics()
is called on thethis
instance implicitly and assumes that the call is within aForm
orControl
type. So, if you are calling it within a control/form, it will just work.Control 类有一个 CreateGraphics 方法。因此,所有控件都有该方法,您可以使用该方法创建图形上下文以在其上进行绘制。
这也是您链接到的帖子所说的内容。请参阅评论:
图形也有一个FromImage 返回 Graphics 对象以在图像上绘制的方法。
The Control class has a CreateGraphics method. Therefore all controls have that method, which you can use to create a graphics context to draw on them.
This is also what the post you linked to says. See the comment :
Graphics also has an FromImage Method that returns a Graphics object to paint on an image.
CreateGraphics 是 System.Windows.Forms.Control 上的一个方法,
您可以这样使用它:
//Create a control:
面板面板 = new Panel();
//从控件中抓取一个图形对象:
图形graphics = panel.CreateGraphics();
//对新创建的 Graphics 对象执行一些操作。
CreateGraphics is a method on System.Windows.Forms.Control
You would use it thusly:
//Create a control:
Panel panel = new Panel();
//Grab a graphics object from the control:
Graphics graphics = panel.CreateGraphics();
//Do something with your newly created Graphics object.