将PictureBox内的图片移动到另一个PictureBox

Ara*_*ash 1 .net c# picturebox winforms

如何将图片框内的图片移动到另一个图片框?

我想用它来移动棋子游戏.我每个地方都有一个拍照框,所以我有64个图片框.

Cod*_*ray 5

您只需将Image一个图片框中显示的内容分配给另一个图片框即可.如果您想要从原始图片框中删除图像(以便它不会显示两次),您可以将其Image属性设置为null(或者,当然,您可以指定您选择的任何其他图像).像这样:

//Assign the image in one picture box to another picture box
mySecondPicBox.Image = myFirstPicBox.Image;

//Clear the image from the original picture box
myFirstPicBox.Image = null;
Run Code Online (Sandbox Code Playgroud)


如果要交换两个图片框中显示的图像,则需要将其中一个图片对象临时存储在变量中.因此,您可以使用非常相似的代码,稍作修改:

//Temporarily store the picture currently displayed in the second picture box
Image secondImage = mySecondPicBox.Image;

//Assign the image from the first picture box to the second picture box
mySecondPicBox.Image = myFirstPicBox.Image;

//Assign the image from the second picture box to the first picture box
myFirstPicBox.Image = secondImage;
Run Code Online (Sandbox Code Playgroud)