如何获取和设置directshow预览分辨率?

时间:2015-06-24 13:50:45

标签: c# .net winforms directshow

这是我添加的表单中的代码:

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 DirectShowLib;
using DirectShowLib.BDA;
using DirectShowLib.DES;
using DirectShowLib.DMO;
using DirectShowLib.Dvd;
using DirectShowLib.MultimediaStreaming;
using DirectShowLib.SBE;
using System.Runtime.InteropServices;
using System.Management;

namespace Youtube_Manager
{

    public partial class Elgato_Video_Capture : Form
    {


        IAMStreamConfig iasc;
        IFilterGraph2 graph;
        ICaptureGraphBuilder2 captureGraph;
        IBaseFilter elgatoFilter;
        IBaseFilter smartTeeFilter;
        IBaseFilter videoRendererFilter;
        Size videoSize;
        string error = "";
        List<Object> devices = new List<Object>();

        public Elgato_Video_Capture()
        {
            InitializeComponent();
            try
            {



                //Set the video size to use for capture and recording
                videoSize = new Size(827, 505);//1280, 720);

                //Initialize filter graph and capture graph
                graph = (IFilterGraph2)new FilterGraph();
                captureGraph = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();
                captureGraph.SetFiltergraph(graph);
                //Create filter for Elgato
                Guid elgatoGuid = new Guid("39F50F4C-99E1-464A-B6F9-D605B4FB5918");
                Type comType = Type.GetTypeFromCLSID(elgatoGuid);
                elgatoFilter = (IBaseFilter)Activator.CreateInstance(comType);
                graph.AddFilter(elgatoFilter, "Elgato Video Capture Filter");

                //Create smart tee filter, add to graph, connect Elgato's video out to smart tee in
                smartTeeFilter = (IBaseFilter)new SmartTee();
                graph.AddFilter(smartTeeFilter, "Smart Tee");
                IPin outPin = GetPin(elgatoFilter, "Video"); 

                IPin inPin = GetPin(smartTeeFilter, "Input");

                graph.Connect(outPin, inPin);

                //Create video renderer filter, add it to graph, connect smartTee Preview pin to video renderer's input pin
                videoRendererFilter = (IBaseFilter)new VideoRenderer();
                graph.AddFilter(videoRendererFilter, "Video Renderer");

                outPin = GetPin(smartTeeFilter, "Preview");

                inPin = GetPin(videoRendererFilter, "Input");
                graph.Connect(outPin, inPin);
                //Render stream from video renderer
                captureGraph.RenderStream(PinCategory.Preview, MediaType.Video, videoRendererFilter, null, null);

                //Set the video preview to be the videoFeed panel
                IVideoWindow vw = (IVideoWindow)graph;
                vw.put_Owner(pictureBox1.Handle);
                vw.put_MessageDrain(this.Handle);
                vw.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipSiblings | WindowStyle.ClipChildren);
                vw.SetWindowPosition(0, 0, 827, 505);//1280, 720);

                //Start the preview
                IMediaControl mediaControl = graph as IMediaControl;
                mediaControl.Run();


            }
            catch (Exception err)
            {
                error = err.ToString();
            }
        }


         IPin GetPin(IBaseFilter filter, string pinname)
        {
            IEnumPins epins;
            int hr = filter.EnumPins(out epins);
            checkHR(hr, "Can't enumerate pins");
            IntPtr fetched = Marshal.AllocCoTaskMem(4);
            IPin[] pins = new IPin[1];
            while (epins.Next(1, pins, fetched) == 0)
            {
                PinInfo pinfo;
                pins[0].QueryPinInfo(out pinfo);
                bool found = (pinfo.name == pinname);
                DsUtils.FreePinInfo(pinfo);
                if (found)
                    return pins[0];
            }
            checkHR(-1, "Pin not found");
            return null;
        }

        public  void checkHR(int hr, string msg)
        {
            if (hr < 0)
            {
                MessageBox.Show(msg);
                DsError.ThrowExceptionForHR(hr);
            }



        }



        private void Elgato_Video_Capture_Load(object sender, EventArgs e)
        {

        }
    }
}

我在一些帖子中读到我需要首先使用IAMStreamConfig::SetFormatGetFormat查找当前格式,然后找到设备/引脚支持的格式/分辨率,最后是{{1我想要的格式(分辨率?)。

但我不明白如何在我的代码中使用它。 我在SetFormat表单顶部的声明之前尝试过,但我不确定在我的代码中如何以及在何处使用它。

修改

我正在使用这种方法:

IAMStreamConfig

当我设置if(i == 4)然后我在变量v上看到分辨率1920 x 1080 但是当我在视频中看到时,图形显得非常平滑。 可能是我的fps太低了,需要改变吗?

这看起来像1920x1080分辨率?

resolution

比较elgato设备640x480(默认)和我的相同分辨率640x480看起来一样吗?

Comparison

修改

我正在使用GetFormat获取当前分辨率,但不确定它是否正确,是否正常工作:

在第一张图之后。我连接了这一行:

public void GetAllAvailableResolution(IPin VideoOutPin)
        {
            IAMStreamConfig streamConfig = (IAMStreamConfig)VideoOutPin;
            AMMediaType searchmedia;
            AMMediaType CorectvidFormat = new AMMediaType();
            IntPtr ptr;

            int piCount, piSize;
            hr = streamConfig.GetNumberOfCapabilities(out piCount, out piSize);
            ptr = Marshal.AllocCoTaskMem(piSize);
            for (int i = 0; i < piCount; i++)
            {
                hr = streamConfig.GetStreamCaps(i, out searchmedia, ptr);
                VideoInfoHeader v = new VideoInfoHeader();
                Marshal.PtrToStructure(searchmedia.formatPtr, v);
                if (i == 4)
                {
                    CorectvidFormat = searchmedia;
                }
            }
            hr = streamConfig.SetFormat(CorectvidFormat); 
        }

我得到的分辨率16x0很奇怪。 X = 0且Y = 16。

改变方法再次结束:

            int hr = 0;
            IntPtr pmt = IntPtr.Zero;
            AMMediaType mediaType = new AMMediaType();
            IAMStreamConfig streamConfig1 = (IAMStreamConfig)outPin;
            hr = streamConfig1.GetFormat(out mediaType);
            VideoInfoHeader v = new VideoInfoHeader();
            Marshal.PtrToStructure(mediaType.formatPtr, v);
            x = v.BmiHeader.Width;
            y = v.BmiHeader.Height;

在底部x和y noth返回值0。 但如果我正在寻找bmih XPelesPerMeter 1280 和YPelesPerMeter 720 可能这是目前使用的解决方案吗? 如果这是为什么它没有把它设置为我想要的? hr = streamConfig.SetFormat(CorectvidFormat);它应该是640x480

1 个答案:

答案 0 :(得分:0)

来自DirectShow.NET Samples

DxWebCam和其他示例显示了如何使用SetFormat。 StackOverflow [12]和MSDN Forums上有很多问题。

在进一步连接/渲染引脚之前,您将获得接口并设置分辨率。