首页 > 解决方案 > 棋盘位图内存不足:如何使棋盘成为单个位图?

问题描述

我正在尝试用 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();
        }

此代码在“Pieces”类下

        public Bitmap GetBitmap()
        {

            return new Bitmap(GetImageLocation());
        }

我希望能够在内存不足的情况下运行代码。

标签: c#bitmapout-of-memorychess

解决方案


推荐阅读