2

I am creating an app with WPF and I need to render a video with transparent background. Does anyone know of a tool or way to do it efficiently?

As far as I know WPF does not natively support rendering videos with transparent background or alpa channel. I have tried to extract each image of the video in a .png and then render it 1 by 1, as you can imagine this is not the most efficient.

Here is the code I am currently using to render the video (it doesn't support transparency)

MediaElement video = new MediaElement
                {
                    Source = new Uri(videoFile, UriKind.RelativeOrAbsolute),
                    Name = objectID,
                    Width = width,
                    Height = height,
                    Opacity = transparency,
                    Volume = mute ? 0 : 1,
                    LoadedBehavior = MediaState.Manual,
                    UnloadedBehavior = MediaState.Stop
                };

                video.MediaOpened += (s, e) =>
                {
                    video.SpeedRatio = 1.0;
                };

                int loopCount = 0;
                video.MediaEnded += (s, e) => 
                {
                    loopCount++;
                    if (loopCount < visibleTime) 
                    {
                        video.Position = TimeSpan.Zero; 
                        video.Play();
                    }
                    else
                    {
                        canvas.Children.Remove(video);
                    }
                };

                MultimediaUtilities.SetStretch(video, stretch);

                Canvas.SetTop(video, y - (height / 2));
                Canvas.SetLeft(video, x - (width / 2));
                Canvas.SetZIndex(video, layer);

                await Task.Delay(delay);

                canvas.Children.Add(video);
                video.Play();
Pedro _F.M
  • 31
  • 2
  • Where do you get the video from? Transparency does not seem to particularly well supported in video formats. If the existing video render does not support transparency you likely will have to find a library, or write your own render. The later will probably be a fair amount of work if you want it to work well. If want a quick and dirty solution you could just update a writeableBitmap for each frame. – JonasH Apr 25 '23 at 12:16
  • Hi, thanks for the help. I am using .avi for the video format. Do you know any library I could use? – Pedro _F.M Apr 25 '23 at 12:46
  • library recommendations go to -> https://softwarerecs.stackexchange.com/ – JonasH Apr 25 '23 at 13:04
  • There is only one way I know how to do this - and I have done it. (1) Obtain the ffmpeg SDK libraries (technically libav); (2) create a custom video player backed by libav which feeds frames to a `D3DImage`. Be prepared to spend months on this and go slightly insane. If anyone knows a better way I'm all ears though! – Emperor Eto Apr 26 '23 at 12:25
  • Actually you could use Windows Media Foundation instead of ffmpeg if WMF supports your codecs. ffmpeg is a lot more versatile. But there is an old but still valid full-blown video player example out there using WMF which could be a starting point. https://learn.microsoft.com/en-us/windows/win32/medfound/how-to-play-unprotected-media-files. Needless to say this involves a lot of C++ and interop. – Emperor Eto Apr 26 '23 at 12:32

0 Answers0