C# 使用图形调整棋盘大小

C# 使用图形调整棋盘大小,c#,graphics,rectangles,C#,Graphics,Rectangles,我为学校做了一个项目,使用图形制作棋盘和数字,当我调整窗体的大小时,这些图形将重新调整大小。我想做两个变量: new\u width=currentFormWidth/startingFormWidth和 new\u height=currentFormHeight/startingFormHeight 。。。在“调整大小”中,将这些宽度和高度与数字和电路板宽度和高度相乘,但由于我使用了矩形-s,所以不能与浮点相乘。还有其他建议吗?下面是代码和屏幕截图 grafik = this.Crea

我为学校做了一个项目,使用
图形制作棋盘和数字,当我调整
窗体的大小时,这些图形将重新调整大小。我想做两个变量:

  • new\u width=currentFormWidth/startingFormWidth
  • new\u height=currentFormHeight/startingFormHeight
。。。在“调整大小”中,将这些宽度和高度与数字和电路板宽度和高度相乘,但由于我使用了
矩形
-s,所以不能与浮点相乘。还有其他建议吗?下面是代码和屏幕截图

 grafik = this.CreateGraphics();
        float new_width = this.Width / startingWidth;
        float new__height = this.Height / startingHeight;
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                if ((j % 2 == 0 && i % 2 == 0) || (j % 2 != 0 && i % 2 != 0))
                    grafik.FillRectangle(new SolidBrush(Color.NavajoWhite), i * 65, j * 65, 65*new_width, 65*new__height);
                else if ((j % 2 == 0 && i % 2 != 0) || (j % 2 != 0 && i % 2 == 0))
                    grafik.FillRectangle(new SolidBrush(Color.DarkRed), i * 65, j * 65, 65 * new_width, 65 * new__height);
            }
        }

        /*************************BLACK FIGURES************************/
        //ROOKS
        grafik.DrawImage(Properties.Resources.figure, new Rectangle(0, 0, 65 * new_width, 65 * new__height), new Rectangle(65 * 2, 2, 65 , 65), GraphicsUnit.Pixel);
        grafik.DrawImage(Properties.Resources.figure, new Rectangle(65 * 7, 0, 65 * new_width, 65 * new__height), new Rectangle(65 * 2, 2, 65 , 65), GraphicsUnit.Pixel);
        //HORSES
        grafik.DrawImage(Properties.Resources.figure, new Rectangle(60, 0, 65 * new_width, 65 * new__height), new Rectangle(65 * 3, 2, 65 , 65), GraphicsUnit.Pixel);
        grafik.DrawImage(Properties.Resources.figure, new Rectangle(65 * 6, 0, 65 * new_width, 65 * new__height), new Rectangle(65 * 3, 2, 65, 65), GraphicsUnit.Pixel);
        //BISHOPS
        grafik.DrawImage(Properties.Resources.figure, new Rectangle(65 * 2, 0, 65 * new_width, 65 * new__height), new Rectangle(65 * 4, 2, 65 , 65), GraphicsUnit.Pixel);
        grafik.DrawImage(Properties.Resources.figure, new Rectangle(65 * 5, 0, 65 * new_width, 65 * new__height), new Rectangle(65 * 4, 2, 65, 65), GraphicsUnit.Pixel);
        //KING AND QUEEN
        grafik.DrawImage(Properties.Resources.figure, new Rectangle(65 * 4, 0, 65 * new_width, 65 * new__height), new Rectangle(0, 2, 65, 65), GraphicsUnit.Pixel);
        grafik.DrawImage(Properties.Resources.figure, new Rectangle(65 * 3, 0, 65 * new_width, 65 * new__height), new Rectangle(65, 2, 65, 65), GraphicsUnit.Pixel);
        //PAWNS
        for (int i = 0; i < 8; i++)
        {
            grafik.DrawImage(Properties.Resources.figure, new Rectangle(65 * i, 65,65*new_width, 65 * new__height), new Rectangle(65 * 5, 2, 65, 65), GraphicsUnit.Pixel);
        }
        /*************************WHITE FIGURES************************/
        //ROOKS
        grafik.DrawImage(Properties.Resources.figure, new Rectangle(0, 65 * 7, 65 * new_width, 65 * new__height), new Rectangle(65 * 2, 65, 65, 65), GraphicsUnit.Pixel);
        grafik.DrawImage(Properties.Resources.figure, new Rectangle(65 * 7, 65 * 7, 65 * new_width, 65 * new__height), new Rectangle(65 * 2, 65, 65, 65), GraphicsUnit.Pixel);
        //HORSES
        grafik.DrawImage(Properties.Resources.figure, new Rectangle(60, 65 * 7, 65 * new_width, 65 * new__height), new Rectangle(65 * 3, 65, 65, 65), GraphicsUnit.Pixel);
        grafik.DrawImage(Properties.Resources.figure, new Rectangle(65 * 6, 65 * 7, 65 * new_width, 65 * new__height), new Rectangle(65 * 3, 65, 65, 65), GraphicsUnit.Pixel);
        //BISHOPS
        grafik.DrawImage(Properties.Resources.figure, new Rectangle(65 * 2, 65 * 7, 65 * new_width, 65 * new__height), new Rectangle(65 * 4, 65, 65, 65), GraphicsUnit.Pixel);
        grafik.DrawImage(Properties.Resources.figure, new Rectangle(65 * 5, 65 * 7, 65 * new_width, 65 * new__height), new Rectangle(65 * 4, 65, 65, 65), GraphicsUnit.Pixel);
        //KING AND QUEEN
        grafik.DrawImage(Properties.Resources.figure, new Rectangle(65 * 4, 65 * 7, 65 * new_width, 65 * new__height), new Rectangle(0, 65, 65, 65), GraphicsUnit.Pixel);
        grafik.DrawImage(Properties.Resources.figure, new Rectangle(65 * 3, 65 * 7, 65 * new_width, 65 * new__height), new Rectangle(65, 65, 65, 65), GraphicsUnit.Pixel);
        //PAWNS
        for (int i = 0; i < 8; i++)
        {
            grafik.DrawImage(Properties.Resources.figure, new Rectangle(65 * i, 65 * 6, 65 * new_width, 65 * new__height), new Rectangle(65 * 5, 65, 65, 65), GraphicsUnit.Pixel);
        }
    }
grafik=this.CreateGraphics();
浮动新宽度=此宽度/起始宽度;
浮动新高度=此高度/起始高度;
对于(int i=0;i<8;i++)
{
对于(int j=0;j<8;j++)
{
如果((j%2==0&&i%2==0)| |(j%2!=0&&i%2!=0))
grafik.FillRectangle(新的SolidBrush(Color.NavajoWhite),i*65,j*65,65*新宽度,65*新高度);
else如果((j%2==0&&i%2!=0)| |(j%2!=0&&i%2==0))
grafik.FillRectangle(新的SolidBrush(Color.DarkRed),i*65,j*65,65*新的宽度,65*新的高度);
}
}
/*************************黑色人物************************/
//车
grafik.DrawImage(Properties.Resources.figure,新矩形(0,0,65*新宽度,65*新高度),新矩形(65*2,2,65,65),GraphicsUnit.Pixel);
grafik.DrawImage(Properties.Resources.figure,新矩形(65*7,0,65*new_宽度,65*new_高度),新矩形(65*2,2,65,65),GraphicsUnit.Pixel);
//马
grafik.DrawImage(Properties.Resources.figure,新矩形(60,0,65*新宽度,65*新高度),新矩形(65*3,2,65,65),GraphicsUnit.Pixel);
grafik.DrawImage(Properties.Resources.figure,新矩形(65*6,0,65*new_宽度,65*new_高度),新矩形(65*3,2,65,65),GraphicsUnit.Pixel);
//主教
grafik.DrawImage(Properties.Resources.figure,新矩形(65*2,0,65*new_宽度,65*new_高度),新矩形(65*4,2,65,65),GraphicsUnit.Pixel);
grafik.DrawImage(Properties.Resources.figure,新矩形(65*5,0,65*new_宽度,65*new_高度),新矩形(65*4,2,65,65),GraphicsUnit.Pixel);
//国王和王后
grafik.DrawImage(Properties.Resources.figure,新矩形(65*4,0,65*new_宽度,65*new_高度),新矩形(0,2,65,65),GraphicsUnit.Pixel);
grafik.DrawImage(Properties.Resources.figure,新矩形(65*3,0,65*new_宽度,65*new_高度),新矩形(65,2,65,65),GraphicsUnit.Pixel);
//典当
对于(int i=0;i<8;i++)
{
grafik.DrawImage(Properties.Resources.figure,新矩形(65*i,65,65*new_宽度,65*new_高度),新矩形(65*5,2,65,65),GraphicsUnit.Pixel);
}
/*************************白色人物************************/
//车
grafik.DrawImage(Properties.Resources.figure,新矩形(0,65*7,65*new_宽度,65*new_高度),新矩形(65*2,65,65,65),GraphicsUnit.Pixel);
grafik.DrawImage(Properties.Resources.figure,新矩形(65*7,65*7,65*new_宽度,65*new_高度),新矩形(65*2,65,65,65),GraphicsUnit.Pixel);
//马
grafik.DrawImage(Properties.Resources.figure,新矩形(60,65*7,65*new_宽度,65*new_高度),新矩形(65*3,65,65,65),GraphicsUnit.Pixel);
grafik.DrawImage(Properties.Resources.figure,新矩形(65*6,65*7,65*new_宽度,65*new_高度),新矩形(65*3,65,65,65),GraphicsUnit.Pixel);
//主教
grafik.DrawImage(Properties.Resources.figure,新矩形(65*2,65*7,65*new_宽度,65*new_高度),新矩形(65*4,65,65,65),GraphicsUnit.Pixel);
grafik.DrawImage(Properties.Resources.figure,新矩形(65*5,65*7,65*new_宽度,65*new_高度),新矩形(65*4,65,65,65,65),GraphicsUnit.Pixel);
//国王和王后
grafik.DrawImage(Properties.Resources.figure,新矩形(65*4,65*7,65*new_宽度,65*new_高度),新矩形(0,65,65,65),GraphicsUnit.Pixel);
grafik.DrawImage(Properties.Resources.figure,新矩形(65*3,65*7,65*new_宽度,65*new_高度),新矩形(65,65,65,65),GraphicsUnit.Pixel);
//典当
对于(int i=0;i<8;i++)
{
grafik.DrawImage(Properties.Resources.figure、新矩形(65*i、65*6、65*new_宽度、65*new_高度)、新矩形(65*5、65、65、65)、GraphicsUnit.Pixel);
}
}

改用。。。。为什么要创建图形对象
grafik=this.CreateGraphics()。。。改为处理
Paint
事件,并使用支持的
e.Graphics
对象进行绘制。Paint事件和图形对象之间的区别是什么?您无法将cat与car buddy进行比较。但是,
Paint
事件在
PaintEventArgs
中提供一个免费的
Graphics
对象供您绘制。因此,您不需要创建额外的
图形
对象,也不需要用它们来填满内存。此外,在某些情况下,当您需要创建任何像
图形
这样的一次性对象时,请确保使用{..}
块在
中创建它,或者确保
.Dispose()当您完成时将其删除。请改用。。。。为什么要创建图形对象
grafik=this.CreateGraphics()。。。改为处理
Paint
事件,并使用支持的
e.Graphics
对象进行绘制。Paint事件和图形对象之间的区别是什么?您无法将cat与car buddy进行比较。但是,
Paint
事件在