看似简单的 .NET 导入

发布于 2025-01-08 06:36:35 字数 1049 浏览 0 评论 0原文

我需要调用这个方法,但在任何命名空间中都找不到它。

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(您是否缺少程序集参考?)”

感谢您的帮助。

---编辑---

我正在尝试运行这篇文章的最佳答案中的代码:

GDI+ / C#:如何将图像另存为 EMF?

---编辑---

根据用户的建议,我正在尝试:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别靠近我心 2025-01-15 06:36:35

using FORMS = System.Windows.Forms;

是一个命名空间别名,而不是类型别名。

命名空间没有定义方法。

此外,CreateGraphics 不是静态的,因此也不能直接在类型上调用,只能在实例上调用。

假设 myFormForm

Graphics g = myForm.CreateGraphics();

在链接的示例中,CreateGraphics()this 实例上隐式调用,并假设该调用在表单控制类型。因此,如果您在控件/表单中调用它,它就会起作用。

This:

using FORMS = System.Windows.Forms;

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 of Form:

Graphics g = myForm.CreateGraphics();

In your linked example, CreateGraphics() is called on the this instance implicitly and assumes that the call is within a Form or Control type. So, if you are calling it within a control/form, it will just work.

远山浅 2025-01-15 06:36:35

Control 类有一个 CreateGraphics 方法。因此,所有控件都有该方法,您可以使用该方法创建图形上下文以在其上进行绘制。

Form frm = new Form();
Graphics g = frm.CreateGraphics();
//you use g to draw on your form frm

这也是您链接到的帖子所说的内容。请参阅评论:

var g = CreateGraphics(); // get a graphics object from your form, or wherever

图形也有一个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.

Form frm = new Form();
Graphics g = frm.CreateGraphics();
//you use g to draw on your form frm

This is also what the post you linked to says. See the comment :

var g = CreateGraphics(); // get a graphics object from your form, or wherever

Graphics also has an FromImage Method that returns a Graphics object to paint on an image.

別甾虛僞 2025-01-15 06:36:35

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文