c#使用system.drawing,更改颜色

c#使用system.drawing,更改颜色,c#,drawing,C#,Drawing,我有一个程序,当我点击绿色方块时,它会变成蓝色并返回。我需要设置“清除”按钮,所以当我按下“清除”按钮时,它应该会变回绿色, 这是我的密码: { Pen myPen = new Pen(Color.Gray); SolidBrush bgBrush=new SolidBrush(Color.Green); SolidBrush pixelBrush=new SolidBrush(Color.Aqua); Int32 rect

我有一个程序,当我点击绿色方块时,它会变成蓝色并返回。我需要设置“清除”按钮,所以当我按下“清除”按钮时,它应该会变回绿色, 这是我的密码:

    {
        Pen myPen = new Pen(Color.Gray);
        SolidBrush bgBrush=new SolidBrush(Color.Green);
        SolidBrush pixelBrush=new SolidBrush(Color.Aqua);
        Int32 rectWidth=80;
        byte[,] BitMap= new byte[8,8];

        public MainForm()
        {
            InitializeComponent();
            for(int i=0;i<8;++i)
                for(int j=0;j<8;++j)
                    BitMap[i,j]=0;
        }
        private void MainFormPaint(object sender, PaintEventArgs e)
        {
            Graphics gdev=e.Graphics;
            SolidBrush currentBrush;
            for(int i=0;i<8;++i)
                for(int j=0;j<8;++j)
                {
                    currentBrush = bgBrush;
                    if (BitMap[i,j]==1)
                    {
                        currentBrush = pixelBrush;
                    }
                    gdev.FillRectangle(currentBrush, i*rectWidth,  j* rectWidth, rectWidth, rectWidth);
                    gdev.DrawRectangle(myPen, i*rectWidth,  j* rectWidth, rectWidth, rectWidth);
                }

        }
        void MainFormClick(object sender, EventArgs e)
        {

        }
        void MainFormMouseClick(object sender, MouseEventArgs e)
        {
            var local = new Point(e.X ,e.Y);
            SolidBrush currentBrush=bgBrush;
            Graphics gdev=Graphics.FromHwnd(this.Handle);
            int i=e.X/rectWidth,j=e.Y/rectWidth;
            if(i>7||j>7)
                return;
            if(BitMap[j,i]==0)
            {
                BitMap[j,i]=1;
                currentBrush=pixelBrush;
            }
            else
            {
                BitMap[j,i]=0;
                currentBrush=bgBrush;
            }
            gdev.FillRectangle(currentBrush, i*rectWidth,  j* rectWidth, rectWidth, rectWidth);
            gdev.DrawRectangle(myPen, i*rectWidth,  j* rectWidth, rectWidth, rectWidth);
            GetHexCode();
        }
        void GetHexCode()
        {
            UInt64 res=0,marker=1;
            for(int i=0;i<8;++i)
                for(int j=0;j<8;++j)
                {
                  res+=BitMap[i,j]*marker;
                  if(i==7&&j==7)
                    break;
                  marker*=2;
                }
            hexFild.Text=res.ToString("X16");
        }
        void SaveButtonClick(object sender, EventArgs e)
        {
            SaveFileDialog MyFileDlg = new SaveFileDialog();
            MyFileDlg.DefaultExt = ".fw";
            MyFileDlg.Filter = "fw data file (*.fw) *.fw|";
            if (MyFileDlg.ShowDialog() == DialogResult.OK)
            {
              MessageBox.Show(MyFileDlg.FileName+" "+hexFild.Text);
              StreamWriter fwStream=new StreamWriter(MyFileDlg.FileName);
              fwStream.Write(hexFild.Text);
              fwStream.Close();
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            this.Invalidate();
        }
{
钢笔myPen=新钢笔(颜色为灰色);
SolidBrush bgBrush=新的SolidBrush(颜色为绿色);
SolidBrush pixelBrush=新的SolidBrush(Color.Aqua);
Int32矩形宽度=80;
字节[,]位图=新字节[8,8];
公共表格(
{
初始化组件();

对于(int i=0;最简单的方法是像在构造函数中一样将位图数组中的所有值重置为零(顺便说一句,这是不必要的,因为开始时所有值都为零),或者只是将其设置为一个新数组,然后触发重绘/无效可以给我一个例子吗?因为我尝试了“for”为了让位图像“MainFormPaint”中那样绘制,但我需要“PaintEventArgs”。我必须使用“EventArgs”,因为我想在按下按钮时清除所有内容必须使您的按钮全部单击事件:
bitmap=new byte[8,8];this.Invalidate();
好的,谢谢,它现在可以工作了