inserting image into codes in chess game c#

我与影子孤独终老i 提交于 2019-12-08 10:30:34

问题


i asked a question about chess game 2days ago,and a friend suggest me the code below,and i have question about it.it was this link

please see: private image Displayimage;

i don't know how should i put images from pieces of chess game in it,and where should i put it? 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



    }
}

回答1:


If you're wondering how you can compile the images along with your source code and then access them, the easiest way is to add the images to your project using Resources. This allows you to easily add external files as embedded resources in your project that will be compiled directly into your executable.

To add a resource to your project, follow these steps:

  1. In the Solution Explorer, right-click the project you want to add a resource to. Select "Properties" option and click the "Resources" tab.
  2. Looking at the toolbar on the top of the Resources window, the first button allows you to select the type of resources that you want to add or edit in your project. In your case, you want to add an image, so select "Image" from the list of options in the drop-down menu.
  3. Then click the drop-down arrow next to the "Add Resource" button. From here, you can either add a new image (which you can draw and edit from within Visual Studio) or add an existing image that you already have on your computer.

Now that you've added resources to your project file, you can use them in your code like this (all of the access details are handled automatically by the ResourceManager class):

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



回答2:


You have to specify image location (resource preferably).
First, Add the image to your resources. Check out this link from MSDN for more info. Then do something like:

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


来源:https://stackoverflow.com/questions/4112769/inserting-image-into-codes-in-chess-game-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!