移动时PictureBox背景等于其他PictureBox?

时间:2016-06-14 16:53:12

标签: c# picturebox

C#初学者。

我正在制作2D坦克游戏,到目前为止,一切都很顺利。 我的两个坦克都是Picture Boxes,我的导弹也是如此。  PictureBoxes中导弹和坦克的图像具有透明的BackColour属性。问题是,导弹和背景的背景。坦克在另一个图片框(pbBackground)顶部不透明。它看起来像this

我知道使用不同的PB是一种效率低下的方法,但我已经走得很远,而且我真的不知道更好。无论如何,正如你所看到的,导弹和坦克PB背景显示了表格颜色。当我下载图像时,背景是透明的,我很确定。如何让我的PB的背景真正透明? (匹配重叠PB的背景?)

我看到了this,但它与我的情景并不相符,我也不了解解决方案。

提前感谢任何帮助人员。

更新:好的,我遵循了Tommy的建议,这是一个正确的方法,可以在不断改变MissileX和MissileY的计时器中沿着pbBackground移动它吗?目前这没有任何作用。

 using (Graphics drawmissile = Graphics.FromImage(pbBackground.Image))
        {
            drawmissile.DrawImage(pbMissile.Image, new Point(MissileX,Convert.ToInt32(MissileY)));
        }

3 个答案:

答案 0 :(得分:2)

PictureBox是不透明的。并且PictureBox效率不高。

为了制作游戏,您应该学习直接在您的表单上绘制的Paint事件。

Bitmap backgroundBitmap = new Bitmap("background");
Bitmap tankBitmap = new Bitmap("tank");

private void Form1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawImage(backgroundBitmap, 0, 0);
    e.Graphics.DrawImage(tankBitmap, 30, 30);
}

private void timer1_Tick(object sender, EventArgs e)
{
    this.Invalidate(); //trigger Form1_Paint to draw next frame
}

答案 1 :(得分:1)

不要将多个PictureBox实例叠加在一起。很快就会变得非常困惑。

相反,使用一个PictureBox并使用Paint将图像绘制到它。通过这种方式,您可以更好地控制正在发生的图形操作。

查看this

$response = json_decode($_POST['s'], true); // decoding received JSON to array
if(is_array($response)){


//start saving now
$order=1;
 foreach ($response as $key => $block) {
  //0 is for blocks: no parent id needed
 $parentid=$this->Blocks_model->insertblock($block['name'],$block['cls'],0,$block['path'],$order);

if (isset($block['children'])) {


   $this->childblocks($parentid,$block['children']);


}

$order++;
}


}

private function childblocks($parentid,$e){
$order=1;
foreach ($e as $key => $block) {
$parentid=$this->Blocks_model->insertblock($block['name'],$block['cls'],0,$block['path'],$order);


if (isset($block['children'])) {

$this->childblocks($parentid,$block['children']);

}
$order++;

}


}

在此示例中,他们演示了如何直接在Form上绘制形状。你会在那里使用你的PictureBox。你也可以画图像。

有很多方法可以使用private void DrawIt() { System.Drawing.Graphics graphics = this.CreateGraphics(); System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle( 50, 50, 150, 150); graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle); graphics.DrawRectangle(System.Drawing.Pens.Red, rectangle); } 对象在表单上绘制形状。尝试查看this问题进行比较。

答案 2 :(得分:0)

Tommy的回答是正确的,但是,如果您使用图片框死设置(一个坏主意),请设置重叠的图片框backgroundcolour到透明和Form的背景到任何图像。 TIL透明BackColour只使用表单颜色/图像。 Tommy 在这里确实有正确的答案,但这就是我为解决问题所做的工作(懒惰的方式)。