2

在我的 Windows 应用程序中,我使用 Windows Media Player dll 播放视频。

在我的表单中,我有一个按钮可以拍摄当前视频帧的照片。

我做了很多测试和代码检查,但我无法找出为什么拍摄当前帧会失败。

我尝试了这段代码,但生成的图像是黑色的:

private Graphics g = null;

private void btnTakePicture_Click(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(axWMVMovie.URL))
    {
        axWMVMovie.Ctlcontrols.pause();
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            System.Drawing.Image ret = null;
            try
            {
                Bitmap bitmap = new Bitmap(axWMVMovie.Width, axWMVMovie.Height);
                {
                    g = Graphics.FromImage(bitmap);
                    {
                        Graphics gg = axWMVMovie.CreateGraphics();
                        {
                            timerTakePicFromVideo.Start();
                        }
                    }

                    using (MemoryStream ms = new MemoryStream())
                    {
                        bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                        ret = System.Drawing.Image.FromStream(ms);
                        ret.Save(saveFileDialog1.FileName);
                    }
                }
            }
            catch
            {
            }
        }
    }
}

private void timerTakePicFromVideo_Tick(object sender, EventArgs e)
{
    timerTakePicFromVideo.Stop();

    g.CopyFromScreen(axWMVMovie.PointToScreen(new System.Drawing.Point()).X, 

    axWMVMovie.PointToScreen(new System.Drawing.Point()).Y, 0, 0,

    new System.Drawing.Size(axWMVMovie.Width, axWMVMovie.Height));
}

我之所以使用Timer,是因为当用户选择保存路径时,函数会从用户指定的文件中获取图像save file dialog。视频格式为 WMV。

4

2 回答 2

4

我拿走了你的代码修改了它。我把捕捉照片的代码放了一点,现在它可以工作了。我在 saveFileDialog 弹出之前创建了图片,因此您实际上只会得到图片而不是图片中的 saveFileDialog。

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);
    }
}
于 2012-10-08T10:11:17.513 回答
0

使用先前的答案,避免获取捕获控件的技巧是在捕获之前执行此操作:

string uimode_previus = axWindowsMediaPlayer2.uiMode;
axWindowsMediaPlayer2.uiMode = "none";

然后当捕获完成后,将uimode背面设置为previous这样:

axWindowsMediaPlayer2.uiMode = uimode_previus ;

这样,您只能从当前帧获得实际拍摄。这是一个小解决方法,但它可以完成工作。

这是工作示例

private void button8_Click_1(object sender, EventArgs e)
{
  string uimode_previus = axWindowsMediaPlayer2.uiMode;
  axWindowsMediaPlayer2.uiMode = "none";

  if (!string.IsNullOrEmpty(axWindowsMediaPlayer2.URL))
  {
    ret = null;
    try
    {
      // take picture BEFORE saveFileDialog pops up!!
      Bitmap bitmap = new Bitmap(axWindowsMediaPlayer2.Width, axWindowsMediaPlayer2.Height);
      {
        Graphics g = Graphics.FromImage(bitmap);
        {
          Graphics gg = axWindowsMediaPlayer2.CreateGraphics();
          {
            //timerTakePicFromVideo.Start();
            this.BringToFront();
            g.CopyFromScreen(axWindowsMediaPlayer2.PointToScreen(
                             new System.Drawing.Point()).X,
                             axWindowsMediaPlayer2.PointToScreen(
                             new System.Drawing.Point()).Y,
                             0, 0,
                             new System.Drawing.Size(
                             axWindowsMediaPlayer2.Width - 0,
                             axWindowsMediaPlayer2.Height - 0)
                            );
          }
        }
        // afterwards save bitmap file if user wants to
        try
        {
          using (MemoryStream ms = new MemoryStream())
          {
            string rute = axWindowsMediaPlayer2.URL.ToString().Replace(".", "Review_."); // 
            bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            ret = new Bitmap(System.Drawing.Image.FromStream(ms));
            ret.Save(rute.Replace(".mp4", ".Png"));
          }

          // open captured frame in new form
          TeamEasy.ShowPictureForm spf = new ShowPictureForm();
          spf.ImagePictureBox.Image = ret;
          spf.ShowDialog();
        }
        catch (Exception ex)
        {
          Debug.WriteLine(ex.Message);
        }
      }
    }
    catch (Exception ex)
    {
      Debug.WriteLine(ex.Message);
    }
  }

  axWindowsMediaPlayer2.uiMode = uimode_previus;
  // restore the UImode of player
}
于 2019-03-18T17:45:01.690 回答