简体   繁体   中英

Drawing a Panels Handle to a Bitmap

I have a WPF-Application which hosts a WinformsPanel

<WindowsFormsHost>
    <windowsForms:Panel 
        x:Name="PlayerHost">
    </windowsForms:Panel>
<WindowsFormsHost>

I then use this Panel to display a video. I'm using Mpv.NET lib to do this. The video player is initialized properly:

//panel.Handle is the windowsForms:Panel named PlayerHost 
player = new MpvPlayer(panel.Handle, Common.IO.FindLib.FindMpvLib(binaryPath));
player.Load(videoFilePath);

Now, if I try to draw the panels content, the resulting image remains blank. The code to draw the image is as follows:

using (var bmp = new Bitmap(panel.ClientSize.Width, panel.ClientSize.Height))
{
    panel.DrawToBitmap(bmp, panel.ClientRectangle);
    bmp.Save(@"Some:\path.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}

To make it clear: The video gets displayed. I can see the video content. And I start drawing to an image when the media is properly loaded. I even offloaded the drawing command to a button click. So while the video was running I tried to take a "frame capture" so to say. But the image remains blank.

How can I capture the panels content? Has it something to do with the native Handle being provided to the video player? Thanks in advance.

You can save the panels content to a bitmap with the following code:

using (Bitmap b = new Bitmap(panel.Width, panel.Height))
{
    panel.DrawToBitmap(b, new Rectangle(0, 0, b.Width, b.Height));
    b.Save("test.png");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM