Thursday, August 23, 2007

RenderFile method Directshow_C#

Render File with C# with Directshow Sample application :



using System;
using System.Collections.Generic;
using System.Text;

using System.Runtime.InteropServices;
using DirectShowLib;

namespace PlayWnd
{
internal enum PlayState
{
Stopped,
Paused,
Running,
Init
};

internal enum MediaType
{
Audio,
Video
}


class PlayWnd
{
private const int WMGraphNotify = 0x0400 + 13;

private IGraphBuilder m_pGraphBuilder = null;
private IMediaControl m_pMediaControl = null;
private IMediaEventEx m_pMediaEventEx = null;
private IMediaSeeking m_pMediaSeeking = null;
private IVideoWindow m_pVideoWindow = null;


public PlayWnd()
{
m_pGraphBuilder = (IGraphBuilder) new FilterGraph();
m_pMediaControl = (IMediaControl) m_pGraphBuilder;
m_pMediaEventEx = (IMediaEventEx) m_pGraphBuilder;
m_pMediaSeeking = (IMediaSeeking) m_pGraphBuilder;

}
~PlayWnd()
{
CloseInterfaces();
}

public int RenderFile(IntPtr handle,string filename)
{
int hr = 0;
hr = m_pGraphBuilder.RenderFile(filename,null);
DsError.ThrowExceptionForHR(hr);
m_pVideoWindow = m_pGraphBuilder as IVideoWindow;
hr = m_pMediaEventEx.SetNotifyWindow(handle, WMGraphNotify, IntPtr.Zero);
DsError.ThrowExceptionForHR(hr);

return 0;
}

public void Start()
{
int hr = 0;
hr = m_pMediaControl.Run();
DsError.ThrowExceptionForHR(hr);
}
public void Pause()
{
int hr = 0;
hr = m_pMediaControl.Pause();
DsError.ThrowExceptionForHR(hr);
}
public void Stop()
{
int hr = 0;
hr = m_pMediaControl.Stop();
DsError.ThrowExceptionForHR(hr);
}

public void SetWindowPosition(IntPtr hwnd,int x, int y, int width, int height)
{
int hr = 0;
if (m_pVideoWindow != null)
{
hr = m_pVideoWindow.SetWindowPosition(
x,
y,
width,
height
);
DsError.ThrowExceptionForHR(hr);
}
hr = m_pVideoWindow.put_Owner(hwnd);
DsError.ThrowExceptionForHR(hr);

hr = m_pVideoWindow.put_WindowStyle(WindowStyle.Child |

WindowStyle.ClipSiblings | WindowStyle.ClipChildren);
DsError.ThrowExceptionForHR(hr);

hr = m_pVideoWindow.put_Visible(OABool.True);
DsError.ThrowExceptionForHR(hr);


}

public void CloseInterfaces()
{
int hr = 0;
try
{
lock (this)
{
hr = this.m_pVideoWindow.put_Visible(OABool.False);
DsError.ThrowExceptionForHR(hr);
hr = this.m_pVideoWindow.put_Owner(IntPtr.Zero);
DsError.ThrowExceptionForHR(hr);

if (m_pMediaEventEx != null)
{
hr = m_pMediaEventEx.SetNotifyWindow(IntPtr.Zero, 0, IntPtr.Zero);
DsError.ThrowExceptionForHR(hr);
}

if (m_pMediaControl != null)
{
m_pMediaControl = null;
}
if (m_pMediaSeeking != null)
{
m_pMediaSeeking = null;
}
if (m_pVideoWindow != null)
{
m_pVideoWindow = null;
}

if (m_pGraphBuilder != null)
{
Marshal.ReleaseComObject(m_pGraphBuilder);
m_pGraphBuilder = null;
}
GC.Collect();
}


}
catch
{
}
}

}
}


Client Usage :
-----------------------
PlayWnd m_pPlayWnd = null;

void btnRender_Click()
{
m_pPlayWnd.RenderFile(pnlVideo.Handle, "D:\\hands.avi");
m_pPlayWnd.SetWindowPosition(pnlVideo.Handle, 0, 0,

pnlVideo.Width, pnlVideo.Height);
}


private void btnStart_Click(object sender, EventArgs e)
{
m_pPlayWnd.Start();
}

private void btnPause_Click(object sender, EventArgs e)
{
m_pPlayWnd.Pause();
}

private void btnStop_Click(object sender, EventArgs e)
{
m_pPlayWnd.Stop();
}

Labels:

0 Comments:

Post a Comment

<< Home