-1

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.)

screenshot of the result

  • Do not confuse Bitmap (a gdi+ object) with bmp (a file format) ! Also: the result of a DrawToBitmap cannot have an alpha channel, even if you declare one, as it is the control surface! Instead draw the layers you want with GDI+ DrawImage yourself! – TaW Sep 07 '19 at 22:22
  • You're encoding with h264, a codec that does not support an Alpha channel, so there's no way to save that data in your video file. You would need a codec that supports an alpha channel, and have support for that codec and alpha channel in the rest of your pipeline. – Anon Coward Sep 07 '19 at 22:33

1 Answers1

1

Like Anon Coward said, the H.264 codec doesn't support alpha channel. Sadly enough none of the codecs from the Accord Video library support alpha channel so I resorted to writing all the frames to a directory and then using FFMPEG to compile them together into a video file. I've used the PNG video codec.

TaW also pointed out that I was confusing the BitMap with bmp, which I fixed by drawing everything to a BitMap, using that BitMap for the frames. To draw on the picturebox I then used DrawImage.

Thanks for the help!