-->

C# Windows Forms capture a image from embeded Wind

2019-05-27 17:29发布

问题:

I have a windows forms application written in C# which uses embedded Windows Media Player (AxInterop.WMPLib.dll and WMPLib.dll) to play some video files. Now I need to add an option to capture image from video on button click. If I set the windowless option to true, I am able to capture an image of a video, but when I set the windowless option to true I don't see a video image on some computers. Without the windowless option I only get a black screen with this code:

        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){ }

How can I capture a frame (snapshot) from a video playing in embedded Windows Media Player without the windowless option in C#?

Or is there any other video player for C# windows forms that can be easily implemented and that supports capture functionality.

回答1:

Hope this code work for you

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);
}

}

One more demo for you : http://www.codeproject.com/Articles/34663/DirectShow-Examples-for-Using-SampleGrabber-for-Gr