IronPython:创造&将一个点数组添加到GraphicsPath

时间:2010-05-31 15:04:30

标签: arrays ironpython point graphicspath

问候;

我在正确实例化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()

根据上面的代码,我希望看到两条垂直的蓝线画出来,每条线末端都有一个小椭圆。使用上面的当前scipting代码,绘制GDI +运行时错误red X.我错过了什么或做错了什么?另外,是否有更简单或更简洁的实例化System.Drawing.Point数组的方法?

提前感谢您的时间和帮助...

1 个答案:

答案 0 :(得分:1)

平心而论,我必须说我没有“回答我自己的问题”或自己解决这个问题,但能够得到Matt Ward和Michael Foord的外界帮助。最真诚的感谢Matt和Michael的时间,帮助和耐心,我非常感谢他们回复他们的更正。

使MainForm.py脚本无法运行的主要问题是我从System.Drawing命名空间导入Color类,以及从System.Drawing.Drawing2D命名空间导入SmoothingMode和LineJoin Enumerations。虽然我的脚本没有直接实例化任何其他枚举或类,但它们仍然必须由.NET DLR从各自的程序集中加载和引用,以使它们在我的脚本中可访问和使用。 (注意:再次感谢Matt指出这一点;如果解释中有任何错误,那么它们是我的,而不是Matt的。)

GDI + Point实例的原始Array实例化是正确的,但更正简洁的方法显示在下面的更正脚本中。 (注意:再次,感谢Michael指出Array实例化替代方案。)

更正和正常工作的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()