2010-05-31 63 views
0

問候;IronPython:創建圖形路徑並將點陣添加到圖形路徑

我正在實例化一個System.Drawing.Point實例的數組,然後在WinForms應用程序中使用IronPython將點數組添加到GDI + GraphicsPath實例中,有點麻煩。下面的代碼編譯或IronPython的2.6的SharpDevelop 3.2下正確地構建:

import System.Drawing 
import System.Drawing.Drawing2D 
import System.Windows.Forms 

from System import Array 
from System.Drawing import Pen, Point 
from System.Drawing.Drawing2D import GraphicsPath, CustomLineCap 
from System.Windows.Forms import * 

class MainForm(Form): 
    def __init__(self): 
     self.InitializeComponent() 

    def InitializeComponent(self): 
     self.SuspendLayout() 
     # 
     # MainForm 
     # 
     self.ClientSize = System.Drawing.Size(284, 264) 
     self.Name = "MainForm" 
     self.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen 
     self.Text = "GDI Lines" 
     self.Paint += self.MainFormPaint 
     self.ResumeLayout(False) 

    def MainFormPaint(self, sender, e): 
     graphicContext = e.Graphics 
     bluePen = Pen(Color.Blue, 1) 

     points = Array.CreateInstance(Point, 9) 
     points[0] = Point(10, 10) 
     points[1] = Point(15, 10) 
     points[2] = Point(20, 15) 
     points[3] = Point(20, 20) 
     points[4] = Point(15, 25) 
     points[5] = Point(10, 25) 
     points[6] = Point(5, 20) 
     points[7] = Point(5, 15) 
     points[8] = Point(10, 10) 

     graphicsPath = GraphicsPath() 
     graphicsPath.AddLines(points) 
     graphicContext.SmoothingMode = SmoothingMode.AntiAlias 

     lineCap = CustomLineCap(nil, graphicsPath) 
     lineCap.BaseInset = 0 
     lineCap.WidthScale = 1 
     lineCap.StrokeJoin = LineJoin.Miter 

     bluePen.CustomStartCap = lineCap 
     bluePen.CustomEndCap = lineCap 

     graphicContext.DrawLine(bluePen, 50, 150, 200, 150) 
     graphicContext.DrawLine(bluePen, 150, 50, 150, 200) 

     lineCap.Dispose() 
     graphicsPath.Dispose() 
     bluePen.Dispose() 

基於上述代碼中,我期待看到兩個perpendicualr藍色線畫出,在每個線的端部的小橢圓。使用上面的當前scipting代碼,繪製GDI +運行時錯誤紅色X.我錯過了什麼或做錯了什麼?另外,是否有更簡單或更簡潔的實例化System.Drawing.Point值數組的方法?

預先感謝您的時間和幫助......

回答

1

平心而論,我必須說,我沒有「回答我的問題」,或解決我自己的這個問題,但能接受來自Matt Ward和Michael Foord的外部幫助。我非常感謝馬特和邁克爾在他們的時間,幫助和耐心,我真的很感謝他們寫下他們的更正。

保持MainForm.py腳本運行的主要問題是在我從System.Drawing命名空間導入Color類以及從System.Drawing.Drawing2D命名空間導入SmoothingMode和LineJoin枚舉時遺漏了部分內容。雖然我的腳本沒有直接實例化任何其他枚舉或類,但它們仍然必須由.NET DLR從它們各自的程序集加載和引用,以使它們在我的腳本中可訪問和可用。 (注意:再次感謝馬特指出這一點;如果解釋中有任何錯誤,他們是我的,而不是馬特的。)

GDI + Point實例的原始數組實例化是正確的,但是下面更正的腳本中顯示了更簡潔的方法。 (注:同樣,我感謝邁克爾指出陣列實例替代)

校正和工作MainForm.py腳本如下:

import System.Drawing 
import System.Drawing.Drawing2D 
import System.Windows.Forms 

from System import Array 
from System.Drawing import Pen, Point, Color 
from System.Drawing.Drawing2D import GraphicsPath, CustomLineCap, SmoothingMode, LineJoin 
from System.Windows.Forms import * 

class MainForm(Form): 
    def __init__(self): 
     self.InitializeComponent() 

    def InitializeComponent(self): 
     self.SuspendLayout() 
     # 
     # MainForm 
     # 
     self.ClientSize = System.Drawing.Size(284, 264) 
     self.Name = "MainForm" 
     self.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen 
     self.Text = "GDI+ CustomLineCaps" 
     self.Paint += self.MainFormPaint 
     self.ResumeLayout(False) 

    def MainFormPaint(self, sender, e): 
     graphics = e.Graphics 
     bluePen = Pen(Color.Blue, 1) 

     points = Array[Point] \ 
     ((Point(10, 10), Point(15, 10), Point(20, 15), \ 
      Point(20, 20), Point(15, 25), Point(10, 25), \ 
      Point(5, 20), Point(5, 15), Point(10, 10))) 

     graphicsPath = GraphicsPath() 
     graphicsPath.AddLines(points) 
     graphics.SmoothingMode = SmoothingMode.AntiAlias 

     lineCap = CustomLineCap(None, graphicsPath) 
     lineCap.BaseInset = 0 
     lineCap.WidthScale = 1 
     lineCap.StrokeJoin = LineJoin.Miter 

     bluePen.CustomStartCap = lineCap 
     bluePen.CustomEndCap = lineCap 

     graphics.DrawLine(bluePen, 50, 150, 200, 150) 
     graphics.DrawLine(bluePen, 150, 50, 150, 200) 

     lineCap.Dispose() 
     graphicsPath.Dispose() 
     bluePen.Dispose()