2

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.

jere_hr
  • 284
  • 3
  • 11

1 Answers1

0

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

Vishal Sen
  • 1,135
  • 1
  • 13
  • 23
  • Thank you for answering, Unfortunately that didn't work. Same as with my code... actually main part of the code is the same. Do you know how to use DirectShow with WMP to capture an image? – jere_hr May 31 '14 at 08:05