I'm trying to record an animation while keeping the transparent background.
I have some animation running in a picturebox with transparent background using windows forms. I convert every frame to a bmp with an alpha channel and then try to stitch them together to obtain a video that still has the transparent background.
As an example I have 60 frames of a square figure moving to the right.
To create the video I've used the Accord Video library. (Accord.Video.FFMPEG
)
public Form1()
{
InitializeComponent();
pictureBox1.Paint += new PaintEventHandler(PictureBox1_Paint);
// create instance of video writer
VideoFileWriter writer = new VideoFileWriter();
// create new video file
writer.Open("test.avi", pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height, 60, VideoCodec.H264);
// create a bitmap to save into the video file
Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
for (int i = 0; i < 60; ++i)
{
pictureBox1.Invalidate();
pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
writer.WriteVideoFrame(bmp);
this.x += 2;
}
writer.Close();
}
private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
SolidBrush brush = new SolidBrush(Color.Blue);
g.FillRectangle(brush, new Rectangle(new Point(x, 50), new Size(50, 50)));
}
private int x = 50;
From what I've read the H.264 codec supports alpha channel, but the result is that it just stacks every single frame on top of eachother and the background turns black (instead of transparent.)