我想移动PictureBox BackgroundImage以获得精灵功能,所以我继承了PictureBox并覆盖了OnPaintBackground:
public class PictureBox1 : PictureBox
{
int bgX = 0,bgY = 0;
public void SetImagePos(int X,int Y) {
bgX = X;
bgY = Y;
Invalidate();
Update();
}
protected override void OnPaintBackground(PaintEventArgs e) {
//base.OnPaintBackground(e);
e.Graphics.DrawImage(BackgroundImage,-bgX,-bgY,BackgroundImage.Width,BackgroundImage.Height);
}
}
但是,这会丢弃透明度。带注释的//base.OnPaintBackground(e);
带回了透明度,但背景还带有原始图像。我也尝试过e.Graphics.Clear(Color.Transparent)
,但背景颜色仍然是天鹅绒黑色。我想念什么?
我想要一个简单的解决方案,性能不是问题。