在国际象棋游戏中将图像插入代码c#

2天前我问了一个关于国际象棋比赛的问题,一位朋友建议我下面的代码,我对它有疑问。 这是这个链接

请参阅:私有图像Displayimage;

我不知道如何将棋子中的棋子放入其中,我应该把它放在哪里? class PiecePosition {

public enum ChessColor { White, Black, } public class ChessPiece { private Image DisplayedImage; private ChessColor DisplayedColor; private Point CurrentSquare; private Point[] ValidMoves; public ChessPiece(Image image, ChessColor color) { DisplayedImage = image; DisplayedColor = color; } } public class KingPiece : ChessPiece { public KingPiece(Image image, ChessColor color) : base(image, color) { ValidMoves[0] = new Point(0, -1); // Up 1 ValidMoves[1] = new Point(1, -1); // Up 1, Right 1 ValidMoves[2] = new Point(1, 0); // Right 1 ValidMoves[7] = new Point(-1, -1); // Left 1, Up 1 } } public class Board { private ChessPiece[,] square; private int SquareWidth; // Number of pixels wide private int SquareHeight; // Number of pixels high } } 

如果您想知道如何将图像与源代码一起编译然后访问它们,最简单的方法是使用Resources将图像添加到项目中。 这使您可以轻松地将外部文件添加为项目中的嵌入式资源,这些资源将直接编译到您的可执行文件中。

要向项目添加资源,请按照下列步骤操作:

  1. 在“解决方案资源管理器”中,右键单击要添加资源的项目。 选择“属性”选项,然后单击“资源”选项卡。
  2. 查看“资源”窗口顶部的工具栏,第一个按钮允许您选择要在项目中添加或编辑的资源类型。 在您的情况下,您想要添加图像,因此从下拉菜单中的选项列表中选择“图像”。
  3. 然后单击“添加资源”按钮旁边的下拉箭头。 从这里,您可以添加新图像(可以在Visual Studio中绘制和编辑),也可以添加计算机上已有的现有图像。

现在您已经为项目文件添加了资源,您可以在代码中使用它们 (所有访问详细信息都由ResourceManager类自动处理):

 System.Drawing.Bitmap kingImage = MyChessGame.Properties.Resources.KingImage; KingPiece kingPiece = new KingPiece(kingImage, ChessColor.White); 

您必须指定图像位置(最好是资源)。
首先,将图像添加到您的资源。 有关详细信息,请查看MSDN中的此链接 。 然后做一些事情:

 var KingImage = WindowsFormsApplication1.Properties.Resources.KingImage; var kingPiece = new KingPiece(KingImage, Color.Black);