C# 棋盘位图内存不足:如何使棋盘成为单个位图?

C# 棋盘位图内存不足:如何使棋盘成为单个位图?,c#,bitmap,out-of-memory,chess,C#,Bitmap,Out Of Memory,Chess,我试图用C#下一盘国际象棋,在移动了几块棋子之后,我总是遇到“内存不足”的问题。我是C#的初学者。我的目标是制作一个有效的国际象棋游戏,棋子只能移动到有效的位置(即棋子只能向前移动,除非它可以移动对角线棋子等)。我知道解决方法是每次移动棋子时将棋盘变成一个位图,而不是一个新的位图,但我不确定如何解决它 我不确定下一步要去哪里,但我知道在继续其他事情之前,我需要解决位图问题。我认为问题在于课堂上的代码。我已将()中图像文件所在的部分更改为GetImageLocation 此代码位于form1下 u

我试图用C#下一盘国际象棋,在移动了几块棋子之后,我总是遇到“内存不足”的问题。我是C#的初学者。我的目标是制作一个有效的国际象棋游戏,棋子只能移动到有效的位置(即棋子只能向前移动,除非它可以移动对角线棋子等)。我知道解决方法是每次移动棋子时将棋盘变成一个位图,而不是一个新的位图,但我不确定如何解决它

我不确定下一步要去哪里,但我知道在继续其他事情之前,我需要解决位图问题。我认为问题在于课堂上的代码。我已将()中图像文件所在的部分更改为GetImageLocation

此代码位于form1下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Checkards
{
    public partial class Form1 : Form
    {
        List<Pieces> chessBoard = new List<Pieces>();

        int move_col = -1;
        int move_row = -1;

        Bitmap DBrown = new Bitmap("E:\\VS\\Checkards\\Images\\DarkBrownSquare.png");
        Bitmap LBrown = new Bitmap("E:\\VS\\Checkards\\Images\\LightBrownSquare.png");

        Pieces.Player current = Pieces.Player.User;

        public Form1()
        {
            InitializeComponent();
            Drawboard();
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            Drawboard();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Drawboard();

            for (int i = 0; i < 8; i++)
            {
                Pieces p = new Pieces(i, 1, Pieces.PieceType.Pawn, Pieces.Player.Computer);
                p.x = i;
                chessBoard.Add(p);
            }
            chessBoard.Add(new Pieces(0, 0, Pieces.PieceType.Rook, Pieces.Player.Computer));
            chessBoard.Add(new Pieces(1, 0, Pieces.PieceType.Knight, Pieces.Player.Computer));
            chessBoard.Add(new Pieces(2, 0, Pieces.PieceType.Bishop, Pieces.Player.Computer));
            chessBoard.Add(new Pieces(3, 0, Pieces.PieceType.Queen, Pieces.Player.Computer));
            chessBoard.Add(new Pieces(4, 0, Pieces.PieceType.King, Pieces.Player.Computer));
            chessBoard.Add(new Pieces(5, 0, Pieces.PieceType.Bishop, Pieces.Player.Computer));
            chessBoard.Add(new Pieces(6, 0, Pieces.PieceType.Knight, Pieces.Player.Computer));
            chessBoard.Add(new Pieces(7, 0, Pieces.PieceType.Rook, Pieces.Player.Computer));

            for(int i = 0; i < 8; i++)
            {
                Pieces p = new Pieces(i, 6, Pieces.PieceType.Pawn, Pieces.Player.User);
                p.x = i;
                chessBoard.Add(p);
            }

            chessBoard.Add(new Pieces(0, 7, Pieces.PieceType.Rook, Pieces.Player.User));
            chessBoard.Add(new Pieces(1, 7, Pieces.PieceType.Knight, Pieces.Player.User));
            chessBoard.Add(new Pieces(2, 7, Pieces.PieceType.Bishop, Pieces.Player.User));
            chessBoard.Add(new Pieces(3, 7, Pieces.PieceType.Queen, Pieces.Player.User));
            chessBoard.Add(new Pieces(4, 7, Pieces.PieceType.King, Pieces.Player.User));
            chessBoard.Add(new Pieces(5, 7, Pieces.PieceType.Bishop, Pieces.Player.User));
            chessBoard.Add(new Pieces(6, 7, Pieces.PieceType.Knight, Pieces.Player.User));
            chessBoard.Add(new Pieces(7, 7, Pieces.PieceType.Rook, Pieces.Player.User));
        }

        public void Drawboard()
        {
            Bitmap pawn = new Bitmap("E:\\VS\\Checkards\\Images\\BlackPawn.png");

            Bitmap landscape = new Bitmap(panel1.Width, panel1.Height);
            Graphics g = Graphics.FromImage(landscape);

            g.Clear(Color.Black);

            float width = panel1.Width / 8.0f;
            float height = panel1.Height / 8.0f;

            for(int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (((i % 2 == 0) && (j % 2 == 0)) || ((i % 2 == 1) && (j % 2 == 1)))
                    {
                        g.DrawImage(LBrown, i * width, j * height, width, height);
                    }
                    else
                    {
                        g.DrawImage(DBrown, i * width, j * height, width, height);
                    }
                }
            }

            for (int i = 0; i < chessBoard.Count; i++)
            {
                g.DrawImage(chessBoard[i].GetBitmap(), chessBoard[i].x * width, chessBoard[i].y * height, width, height);
            }

            Graphics g2 = panel1.CreateGraphics();
            g2.DrawImage(landscape, 0, 0);
            landscape.Dispose();
            g.Dispose();
        }

我希望能够在没有内存耗尽的情况下运行代码。

1-当使用GDI资源(或任何IDisposable项)时,您需要处理它。建议使用
Using
子句。如果你泄露位图2,OOT会很快击中你-为了持久的结果,千万不要使用
control.CreateGraphics
!切勿尝试缓存
图形
对象!使用
Graphics g=Graphics.FromImage(bmp)
或在控件的
Paint
事件中,使用
e.Graphics
参数将bmp绘制到
位图中您可以通过执行最小化/最大化序列来测试图形的持久性..1-当使用GDI资源(或任何IDisposable项)时,您需要处理它。建议使用
Using
子句。如果你泄露位图2,OOT会很快击中你-为了持久的结果,千万不要使用
control.CreateGraphics
!切勿尝试缓存
图形
对象!使用
Graphics g=Graphics.FromImage(bmp)
或在控件的
Paint
事件中,使用
e.Graphics
参数将bmp绘制到
位图中您可以通过执行最小化/最大化序列来测试图形的持久性。。
        public Bitmap GetBitmap()
        {

            return new Bitmap(GetImageLocation());
        }