问题
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:
- In the Solution Explorer, right-click the project you want to add a resource to. Select "Properties" option and click the "Resources" tab.
- 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.
- 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