2012-02-22 32 views
0

我需要調用此方法,但我無法在任何名稱空間中找到它。看似簡單的.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 

錯誤是:「類型或命名空間名稱‘的createGraphics’不存在命名空間存在「System.Windows.Forms的(您是否缺少裝配參考?)「

感謝您的任何幫助。

---編輯---

我試圖運行從頂端回答這個職位代碼:

GDI+/C#: How to save an image as EMF?

---編輯---

隨着用戶的建議,我試圖:

Graphics g = new Graphics(); 
FORMS.Control a = new FORMS.Control(); 
g = a.CreateGraphics(); 

---編輯---

沒關係,對不起人,我很蠢。將在一秒鐘內接受。

+0

那麼你鏈接你的遊戲假設你將你的代碼放在一個表單中。 – 2012-02-22 16:46:51

+0

@Boo,哦,讓我不完全讀它。 – jn1kk 2012-02-22 16:48:36

回答

4

此:

using FORMS = System.Windows.Forms; 

是一個命名空間別名,不是一個類型別名。

命名空間沒有定義它們的方法。

此外,CreateGraphics而不是靜態,因此不能直接調用類型,只能在一個實例上調用。

假設myFormForm一個實例:

Graphics g = myForm.CreateGraphics(); 

在你的鏈接例如,CreateGraphics()被稱爲上隱含的this實例,並認爲該呼叫是FormControl類型中。所以,如果你在一個控件/表單中調用它,它就會工作。

0

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 

Graphics也有一個FromImage方法返回一個Graphics對象繪製的圖像上。

0

的createGraphics是System.Windows.Forms.Control的

方法

你會正是如此使用它:

//創建控制: 面板面板=新面板();

//從控件中獲取一個圖形對象: Graphics graphics = panel.CreateGraphics();

//用你新創建的Graphics對象做一些事情。

相關問題