'Play video using DirectX in C# WinForm
I want to achieve almost something similar here. I want a control that supports both images or video. This will play for some duration.
I am able to show the video using WMP(Windows media player) object. But now I am trying to use DirectX to render the images or video.
Please help me on this. Suggestion are welcomed.
Here is what I tried.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.DirectX.AudioVideoPlayback;
using Microsoft.DirectX;
namespace WindowsPlayerDirectX
{
  public partial class Form1 : Form
  {
     private Video video;
  public Form1()
{
    InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
    //int height = panel1.Height;
    //int width = panel1.Width;
    try
    {
        MessageBox.Show("Hi", "Test");
        openFileDialog1.Filter = "Image Files(jpg,png,bmp,gif)|*.jpg;*.jpeg;*.png;*.bmp;*.gif|all files|*.*";
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
          //   video = new Video(openFileDialog1.FileName,false);
            pictureBox1.Load(openFileDialog1.FileName);
        }
    }catch (Exception ex)
    {
        MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
    }
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
    //video = new Video("D:\\Wildlife.wmv", false);
    //video.Owner = panel1;
    //video.Stop();
    //video.Dispose();
    //if (!video.Playing)
    //    video.Play();
    //else
    //    MessageBox.Show("File already playing D:\\Wildlife.wmv ", "Info");
    //OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog1.ShowDialog();
    openFileDialog1.Title = "Select video file..";
    openFileDialog1.InitialDirectory = Application.StartupPath;
    openFileDialog1.DefaultExt = ".avi";
    openFileDialog1.Filter = "Media Files|*.mpg;*.avi;*.wma;*.mov;*.wav;*.mp2;*.mp3|All Files|*.*";
    video = new Video(openFileDialog1.FileName);
    video.Owner = panel1;
    panel1.Width = 700;
    panel1.Height = 390;
   }
   }}
Solution 1:[1]
I'm not by any means an expert in DirectX applications, but I've made one with OpenGL...
I guess what's missing is some kind of Surface to render the video into.
BTW, it seems that Microsoft DirectX in .Net is deprecated. I guess that you'll be better off using SharpDx.
Solution 2:[2]
you forgot to call video.Play();
or you can add autopaly in constructor like this:
video = new Video(openFileDialog1.FileName, true);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | TheXDS | 
| Solution 2 | CatYoutuber_ | 
