How to List Available Video Modes Using C#

How to list available video modes using C#?

If you mean video modes are available resolutions, try to invoke EnumDisplaySettingsEx

details can be found here:

http://msdn.microsoft.com/en-us/library/dd162612(VS.85).aspx

small program that lists available resolutions:

using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ListResolutions
{

class Program
{
[DllImport("user32.dll")]
public static extern bool EnumDisplaySettings(
string deviceName, int modeNum, ref DEVMODE devMode);
const int ENUM_CURRENT_SETTINGS = -1;

const int ENUM_REGISTRY_SETTINGS = -2;

[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{

private const int CCHDEVICENAME = 0x20;
private const int CCHFORMNAME = 0x20;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public int dmPositionX;
public int dmPositionY;
public ScreenOrientation dmDisplayOrientation;
public int dmDisplayFixedOutput;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public int dmICMMethod;
public int dmICMIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;
public int dmPanningWidth;
public int dmPanningHeight;

}

static void Main(string[] args)
{
DEVMODE vDevMode = new DEVMODE();
int i = 0;
while (EnumDisplaySettings(null, i, ref vDevMode))
{
Console.WriteLine("Width:{0} Height:{1} Color:{2} Frequency:{3}",
vDevMode.dmPelsWidth,
vDevMode.dmPelsHeight,
1 << vDevMode.dmBitsPerPel, vDevMode.dmDisplayFrequency
);
i++;
}
}

}

}

How to list camera available video resolution

This is a code that I wrote, its working perfectly for me

public static List<Point> GetAllAvailableResolution(DsDevice vidDev)
{
try
{
int hr;
int max = 0;
int bitCount = 0;
IBaseFilter sourceFilter = null;
var m_FilterGraph2 = new FilterGraph() as IFilterGraph2;
hr = m_FilterGraph2.AddSourceFilterForMoniker(vidDev.Mon, null, vidDev.Name, out sourceFilter);
var pRaw2 = DsFindPin.ByCategory(sourceFilter, PinCategory.Capture, 0);
var AvailableResolutions = new List<Point>();
VideoInfoHeader v = new VideoInfoHeader();
IEnumMediaTypes mediaTypeEnum;
hr = pRaw2.EnumMediaTypes(out mediaTypeEnum);
AMMediaType[] mediaTypes = new AMMediaType[1];
IntPtr fetched = IntPtr.Zero;
hr = mediaTypeEnum.Next(1, mediaTypes, fetched);

while (fetched != null && mediaTypes[0] != null)
{
Marshal.PtrToStructure(mediaTypes[0].formatPtr, v);
if (v.BmiHeader.Size != 0 && v.BmiHeader.BitCount != 0)
{
if (v.BmiHeader.BitCount > bitCount)
{
AvailableResolutions.Clear();
max = 0;
bitCount = v.BmiHeader.BitCount;
}
AvailableResolutions.Add(new Point(v.BmiHeader.Width, v.BmiHeader.Height));
if (v.BmiHeader.Width > max || v.BmiHeader.Height > max)
max = (Math.Max(v.BmiHeader.Width, v.BmiHeader.Height));
}
hr = mediaTypeEnum.Next(1, mediaTypes, fetched);
}
return AvailableResolutions;
}

catch (Exception ex)
{
Log(ex);
return new List<Point>();
}
}

(E.g. this can be added to VideoCaptureElement in WPF-MediaKit)

How to get supported resolution of a specific monitor?

You should enumerate all display devices with EnumDisplayDevices API and then use DeviceName as first parameter for EnumDisplaySettings API, here is the code to get display device names:

  var displayDeviceNames = new List<string>();
int deviceIndex = 0;
while (true)
{
var deviceData = new DisplayDevice();
deviceData.cb = Marshal.SizeOf(typeof(DisplayDevice));
if (EnumDisplayDevices(null, deviceIndex, ref deviceData, 0) != 0)
{
displayDeviceNames.Add(deviceData.DeviceName);
deviceIndex++;
}
else
{
break;
}
}

Needed declarations:

[Flags()]
public enum DisplayDeviceStateFlags : int
{
/// <summary>The device is part of the desktop.</summary>
AttachedToDesktop = 0x1,
MultiDriver = 0x2,
/// <summary>The device is part of the desktop.</summary>
PrimaryDevice = 0x4,
/// <summary>Represents a pseudo device used to mirror application drawing for remoting or other purposes.</summary>
MirroringDriver = 0x8,
/// <summary>The device is VGA compatible.</summary>
VGACompatible = 0x16,
/// <summary>The device is removable; it cannot be the primary display.</summary>
Removable = 0x20,
/// <summary>The device has more display modes than its output devices support.</summary>
ModesPruned = 0x8000000,
Remote = 0x4000000,
Disconnect = 0x2000000
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DisplayDevice
{
[MarshalAs(UnmanagedType.U4)]
public int cb;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceString;
[MarshalAs(UnmanagedType.U4)]
public DisplayDeviceStateFlags StateFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceKey;
}

[DllImport("User32.dll")]
static extern int EnumDisplayDevices(string lpDevice, int iDevNum, ref DisplayDevice lpDisplayDevice, int dwFlags);

How to get list of available screen resolutions?

This answer has a working sample

How to list available video modes using C#?

Available Resolutions For a specific Screen

In the first of those links you're told about EnumDisplaySettings. Take a few seconds to lookup that function and the FIRST PARAMETER is the

string that specifies the display device about whose graphics mode the
function will obtain information.

Here's a sample class to fetch information about displays. I've deliberately omitted DEVMODE since you've already got it.

public class NativeMethods
{
[DllImport("user32.dll")]
public static extern bool EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);
[DllImport("user32.dll")]
public static extern bool EnumDisplayDevices(string deviceName, int modeNum, ref DISPLAY_DEVICE displayDevice, int flags);
}

[StructLayout(LayoutKind.Sequential)]
public struct DISPLAY_DEVICE
{
public int cb;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceString;
public int StateFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceKey;
}

static class Display
{
public static List<DISPLAY_DEVICE> GetGraphicsAdapters()
{
int i = 0;
DISPLAY_DEVICE displayDevice = new DISPLAY_DEVICE();
List<DISPLAY_DEVICE> result = new List<DISPLAY_DEVICE>();
displayDevice.cb = Marshal.SizeOf(displayDevice);
while (NativeMethods.EnumDisplayDevices(null, i, ref displayDevice, 1))
{
result.Add(displayDevice);
i++;
}

return result;
}

public static List<DISPLAY_DEVICE> GetMonitors(string graphicsAdapter)
{

DISPLAY_DEVICE displayDevice = new DISPLAY_DEVICE();
List<DISPLAY_DEVICE> result = new List<DISPLAY_DEVICE>();
int i = 0;
displayDevice.cb = Marshal.SizeOf(displayDevice);
while (NativeMethods.EnumDisplayDevices(graphicsAdapter, i, ref displayDevice, 0))
{
result.Add(displayDevice);
i++;
}

return result;
}

public static List<DEVMODE> GetDeviceModes(string graphicsAdapter)
{
int i = 0;
DEVMODE devMode = new DEVMODE();
List<DEVMODE> result = new List<DEVMODE>();
while (NativeMethods.EnumDisplaySettings(graphicsAdapter, i, ref devMode))
{
result.Add(devMode);
i++;
}
return result;
}
}

How to get a list of available resolutions for every camera on Windows C++

Also, I don't care which API to use, be it DirectShow or MMF (Microsoft Media Foundation). I haven't used any of these before either.

You generally do care because the data might be different.

With Media Foundation, see How to Set the Video Capture Format

Call IMFMediaTypeHandler::GetMediaTypeCount to get the number of supported formats.

You might also want to have a look at Tanta:
Windows Media Foundation Sample Projects and sample code there.

With DirectShow, see Webcamera supported video formats in addition to links you found; you will have to sort out includes and libraries through, among SDK samples AMCap does some one this and can be built from source without additional external libraries, from original code or from this fork adopted to most recent VS.

How Do I get list of audio/video and capture devices in C#?

After Googling for "C# get video capture devices". I ended up at these two CodeProject articles:

  • DirectShow.NET
  • DirectX.Capture Class Library

You'll see that there is a lot of COM interop involved. And from the sound of it, I'm not sure you're ready to jump right into that, at it can be a lot of tedious work. I would consider using what is there, and not re-inventing the wheel. After all, they are both in the public domain.

Additionally, there is this post on StackOverflow which has some interesting links:

  • Get default audio/video device

List of valid resolutions for a given Screen?

I think it should be possible to get the information using Windows Management Instrumentation (WMI). WMI is accessible from .NET using the classes from them System.Management namespace.

A solution will look similar to the following. I don't know WMI well and could not immediately find the information you are looking for, but I found the WMI class for the resolutions supported by the video card. The code requires referencing System.Management.dll and importing the System.Management namespace.

var scope = new ManagementScope();

var query = new ObjectQuery("SELECT * FROM CIM_VideoControllerResolution");

using (var searcher = new ManagementObjectSearcher(scope, query))
{
var results = searcher.Get();

foreach (var result in results)
{
Console.WriteLine(
"caption={0}, description={1} resolution={2}x{3} " +
"colors={4} refresh rate={5}|{6}|{7} scan mode={8}",
result["Caption"], result["Description"],
result["HorizontalResolution"],
result["VerticalResolution"],
result["NumberOfColors"],
result["MinRefreshRate"],
result["RefreshRate"],
result["MaxRefreshRate"],
result["ScanMode"]);
}
}


Related Topics



Leave a reply



Submit