表单上有许多动态创建的图片框。它们都是某些面板的子控件。我要显示的图像具有 Alpha 通道。问题是 alpha 通道的解释不正确。
设置属性BackColor没有帮助:PictureBoxes 属于一个面板,所以背景是一个白色的面板。
在网上找了一个工作方法,但是不行(图片不显示):
public class PictureBoxTW : PictureBox
{
protected override void OnPaintBackground(PaintEventArgs e)
// Paint background with underlying graphics from other controls
{
base.OnPaintBackground(e);
Graphics g = e.Graphics;
if (Parent != null)
{
// Take each control in turn
int index = Parent.Controls.GetChildIndex(this);
for (int i = Parent.Controls.Count - 1; i > index; i--)
{
Control c = Parent.Controls[i];
// Check it's visible and overlaps this control
if (c.Bounds.IntersectsWith(Bounds) && c.Visible)
{
// Load appearance of underlying control and redraw it on this background
Bitmap bmp = new Bitmap(c.Width, c.Height, g);
c.DrawToBitmap(bmp, c.ClientRectangle);
g.TranslateTransform(c.Left - Left, c.Top - Top);
g.DrawImageUnscaled(bmp, Point.Empty);
g.TranslateTransform(Left - c.Left, Top - c.Top);
bmp.Dispose();
}
}
}
}
}
有没有人有现成的方法来解决这个问题,我不需要用铃鼓跳舞?
Koso da lomo 解决了这个问题,但我不得不丢弃任何 PictureBox,接受双缓冲的事实并直接在表单上绘图。以下是实现类的工作片段。
也就是说,每当其中任何一个的动画帧发生变化时,我都会在表单上绝对绘制所有透明图片框对象。
我不认为我会喜欢它。这种方法有很多缺点,需要大量的外部控制。