如何在C#中播放电影并使用DirectX添加文本叠加层?

时间:2010-07-03 06:05:46

标签: c#

如何在C#中播放电影并使用DirectX添加文本叠加,请指定一些参考,因为我是这方面的新手。

3 个答案:

答案 0 :(得分:0)

用于视频播放的DirectX的特定部分称为DirectShow,您需要有关如何使用来自c#的DirectShow的参考,一个起点可能就是这个:

http://www.codeproject.com/KB/directx/directshowmediaplayer.aspx

答案 1 :(得分:0)

有关在DirectX表面上显示文本的一些源代码,请参阅http://www.riemers.net/eng/Tutorials/DirectX/Csharp/Series2/tut18.php ...

答案 2 :(得分:0)

这是一个初始化Direct3D,创建几个三角形顶点并在其上显示视频的表单。因为我对顶点没有理解,所以它没有完全正确排列,但是它成功地加载了视频并将其作为纹理渲染到三角形上。

public partial class Form1 : Form
{   
    public Form1()
    {
        InitializeComponent();
    }

    private Microsoft.DirectX.Direct3D.Device _device;
    private Microsoft.DirectX.Direct3D.Font font;
    private Microsoft.DirectX.AudioVideoPlayback.Video _video;

    CustomVertex.PositionTextured[] vertices;

    public void Init()
    {
        PresentParameters present_params = new PresentParameters();
        present_params.Windowed = true;
        present_params.SwapEffect = SwapEffect.Discard;

        _device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, present_params);

        font = new Microsoft.DirectX.Direct3D.Font(_device, 20, 0, FontWeight.Bold, 0, false, CharacterSet.Default, Precision.Default, FontQuality.Default, PitchAndFamily.DefaultPitch, "Arial");

        _video = new Video("clock.avi");
        _video.TextureReadyToRender += new TextureRenderEventHandler(_video_TextureReadyToRender);
        _video.RenderToTexture(_device);

        vertices = new CustomVertex.PositionTextured[6];
        vertices[0].Position = new Vector3(1f, 1f, 0f);
        vertices[0].Tu = 0;
        vertices[0].Tv = 1;
        vertices[1].Position = new Vector3(-1f, -1f, 0f);
        vertices[1].Tu = 1;
        vertices[1].Tv = 1;
        vertices[2].Position = new Vector3(1f, -1f, 0f);
        vertices[2].Tu = 0;
        vertices[2].Tv = 0;
        vertices[3].Position = new Vector3(-1.1f, -0.99f, 0f);
        vertices[3].Tu = 1;
        vertices[3].Tv = 0;
        vertices[4].Position = new Vector3(0.99f, 1.1f, 0f);
        vertices[4].Tu = 0;
        vertices[4].Tv = 0;
        vertices[5].Position = new Vector3(-1.1f, 1.1f, 0f);
        vertices[5].Tu = 1;
        vertices[5].Tv = 1;
    }
    void _video_TextureReadyToRender(object sender, TextureRenderEventArgs e)
    {
        _device.BeginScene();
        _device.SetTexture(0, e.Texture);
        _device.VertexFormat = CustomVertex.PositionTextured.Format;
        _device.DrawUserPrimitives(PrimitiveType.TriangleList, 2, vertices);

        font.DrawText(null, "test overlay", 5, 5, Color.Red);

        _device.EndScene();
        _device.Present();
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        _device.Clear(ClearFlags.Target, Color.Blue, 0.0F, 0);

    }
    private void Form1_Load(object sender, EventArgs e)
    {
        _video.Play();
    }
}

您需要将场景绘图代码放在TextureReadyToRender事件中,否则您的视频将永远不会显示。