jn1kk
jn1kk

Reputation: 5102

Seemingly Easy .NET Import

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.

Upvotes: 0

Views: 557

Answers (3)

Oded
Oded

Reputation: 499062

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.

Upvotes: 4

Sam Axe
Sam Axe

Reputation: 33738

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.

Upvotes: 0

gideon
gideon

Reputation: 19465

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.

Upvotes: 0

Related Questions