C#Windows窗体从嵌入式Windows Media Player捕获图像

时间:2014-05-31 07:06:11

标签: c# winforms video wmp

我有一个用C#编写的Windows窗体应用程序,它使用嵌入式Windows Media Player(AxInterop.WMPLib.dll和WMPLib.dll)播放一些视频文件。现在我需要添加一个选项,以便在按钮点击时从视频中捕获图像。如果我将无窗口选项设置为true,我可以捕获视频图像,但是当我将无窗口选项设置为true时,我在某些计算机上看不到视频图像。如果没有无窗口选项,我只能使用以下代码获得黑屏:

        System.Drawing.Image ret = null;
        try{
            Bitmap bitmap = new Bitmap(wmPlayer.Width-26, wmPlayer.Height-66);
            {
                Graphics g = Graphics.FromImage(bitmap);
                {
                    Graphics gg = wmPlayer.CreateGraphics();
                    {
                        this.BringToFront();

                           g.CopyFromScreen(
                            wmPlayer.PointToScreen(
                                new System.Drawing.Point()).X+13,
                            wmPlayer.PointToScreen(
                                new System.Drawing.Point()).Y,
                            0, 0,
                            new System.Drawing.Size(
                                wmPlayer.Width-26,
                                wmPlayer.Height-66)  
                            );
                    }
                }
                using (MemoryStream ms = new MemoryStream()){
                        bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                        ret = System.Drawing.Image.FromStream(ms);
                        ret.Save(@"C:\\WMP_capture.png");
                        pictureBox1.Image=ret;
                }
            }
            bitmap.Dispose();

        }catch (Exception){ }

如何在没有C#的无窗口选项的情况下从嵌入式Windows Media Player中播放的视频中捕获帧(快照)?

或者是否还有其他适用于C#窗体的视频播放器,可以轻松实现并支持捕获功能。

1 个答案:

答案 0 :(得分:0)

希望此代码适合您

if (!string.IsNullOrEmpty(axWindowsMediaPlayer1.URL)){
axWindowsMediaPlayer1.Ctlcontrols.pause();

System.Drawing.Image ret = null;
try
{
    // take picture BEFORE saveFileDialog pops up!!
    Bitmap bitmap = new Bitmap(axWindowsMediaPlayer1.Width, axWindowsMediaPlayer1.Height);
    {
        Graphics g = Graphics.FromImage(bitmap);
        {
            Graphics gg = axWindowsMediaPlayer1.CreateGraphics();
            {
                //timerTakePicFromVideo.Start();
                this.BringToFront();
                g.CopyFromScreen(
                    axWindowsMediaPlayer1.PointToScreen(
                        new System.Drawing.Point()).X,
                    axWindowsMediaPlayer1.PointToScreen(
                        new System.Drawing.Point()).Y,
                    0, 0,
                    new System.Drawing.Size(
                        axWindowsMediaPlayer1.Width,
                        axWindowsMediaPlayer1.Height)
                    );
            }
        }
        // afterwards save bitmap file if user wants to
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                ret = System.Drawing.Image.FromStream(ms);
                ret.Save(saveFileDialog1.FileName);
            }
        }
    }
}
catch (Exception ex)
{
    Debug.WriteLine(ex.Message);
}

}

另外一个演示:http://www.codeproject.com/Articles/34663/DirectShow-Examples-for-Using-SampleGrabber-for-Gr