如何在c#中更改笔的颜色?

如何在c#中更改笔的颜色?,c#,windows-forms-designer,color-picker,pen,solidcolorbrush,C#,Windows Forms Designer,Color Picker,Pen,Solidcolorbrush,我正试图改变我的钢笔和纯色画笔的颜色 我对它们进行了硬编码,但现在我想知道如何使用GUI(下拉列表、调色板等)只制作一支钢笔并将颜色更改为我想要的任何颜色。 这是我的密码: private void imageComboBox_SelectedIndexChanged(object sender, EventArgs e) { // create graphics object, Pen and SolidBrush Graphics myGrap

我正试图改变我的钢笔和纯色画笔的颜色

我对它们进行了硬编码,但现在我想知道如何使用GUI(下拉列表、调色板等)只制作一支钢笔并将颜色更改为我想要的任何颜色。

这是我的密码:

    private void imageComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        // create graphics object, Pen and SolidBrush
        Graphics myGraphics = base.CreateGraphics();

        // create Pen using different colors
        Pen myPen = new Pen(Color.DarkRed);
        Pen myPen2 = new Pen(Color.Blue);
        Pen myPen3 = new Pen(Color.Purple);
        Pen myPen4 = new Pen(Color.DarkGreen);

        // create SolidBrush using color DarkRed
        SolidBrush mySolidBrush = new SolidBrush(Color.DarkRed);
        SolidBrush mySolidBrush2 = new SolidBrush(Color.Orange);
        SolidBrush mySolidBrush3 = new SolidBrush(Color.Blue);
        SolidBrush mySolidBrush4 = new SolidBrush(Color.Green);

        // clear drawing area setting it to color white
        myGraphics.Clear(Color.White);

        // find index, draw proper shape
        switch (imageComboBox.SelectedIndex)
        {
            case 0: // case Circle is selected
                myGraphics.DrawEllipse(myPen, 50, 50, 150, 150);
                break;
            case 1: // case Rectangle is selected
                myGraphics.DrawRectangle(myPen2, 50, 50, 150, 150);
                break;
            case 2: // case Ellipse is selected
                myGraphics.DrawEllipse(myPen3, 50, 85, 150, 115);
                break;
            case 3: // case Pie is selected
                myGraphics.DrawPie(myPen4, 50, 50, 150, 150, 0, 45);
                break;
            case 4: // case Filled Circle is selected
                myGraphics.FillEllipse(mySolidBrush, 50, 50, 150, 150);
                break;
            case 5: // case Filled Rectangle is selected
                myGraphics.FillRectangle(mySolidBrush2, 50, 50, 150,
                   150);
                break;
            case 6: // case Filled Ellipse is selected
                myGraphics.FillEllipse(mySolidBrush3, 50, 85, 150, 115);
                break;
            case 7: // case Filled Pie is selected
                myGraphics.FillPie(mySolidBrush4, 50, 50, 150, 150, 0,
                   45);
                break;}
        myGraphics.Dispose(); // release the Graphics object
    }

您可以使用
Pen
color
属性更改
笔的颜色,如下所示:

Pen p = new Pen();
p.Color = Color.Red;

然后您可以在每种情况下使用
p.Color=…
,并使用相同的笔。

如果您只使用其中一支笔,为什么要创建四支笔?你不应该处置它们吗?