WinForm摄像头 录像 格式化问题
用的是 avicap32.dll
我也是用的网上的方法,录像已经可以了。关键是我要设置分辨率。
别给我链接地址OK?
//=====================================================CAPSTATUS结构========================================================================
//CAPSTATUS定义了捕获窗口的当前状态,如图像的宽、高等;
[StructLayout(LayoutKind.Sequential)]
public struct CAPSTATUS
{
public int uiImageWidth; //图像宽度
public int uiImageHeight; //图像高度
public bool fLiveWindow; //活动窗口标记,如果窗口正以预览的方式展示图像,那么该值为真
public bool fOverlayWindow; //叠加窗口标志位,如果正在使用硬件叠加,则该位是真。
public bool fScale; //输入所放标志位,如果窗口是正在缩放视频到客户区,那么该位是真。当使用硬件叠加时,改位无效。
public Point ptScroll; //被展示在窗口客户区左上角的那个象素的x、y坐标偏移量。
public bool fUsingDefaultPalette; //默认调色板标志位,如果捕获窗口正在使用当前默认调色板,该值为真
public bool fAudioHardware; // 音频硬件标志位,如果系统已经安装了音频硬件,该值为真。
public bool fCapFileExists; //捕获文件标志位,如果一个捕获文件已经被创建,该值为真
public int dwCurrentVideoFrame; // 当前或最近流捕获过程中,所处理的桢的数目。包括丢弃的桢。
public int dwCurrentVideoFramesDropped;//当前流捕获过程中丢弃的桢的数目。
public int dwCurrentWaveSamples; // # of wave samples cap'td
public int dwCurrentTimeElapsedMS; // 从当前流捕获开始计算,程序所用的时间,以毫秒为单位。
public IntPtr hPalCurrent; // 当前剪切板的句柄。
public bool fCapturingNow; // 捕获标志位,当捕获是正在进行时,改位是真
public int dwReturn; // 错误返回值,当你的应用程序不支持错误回调函数时可以应用改位
public int wNumVideoAllocated; // 被分配的视频缓存的数目。
public int wNumAudioAllocated; // 被分配的音频缓存的数目。
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace 解决分辨率
{
public partial class Form1 : Form
{
//保存当前屏幕分辨率
int i = Screen.PrimaryScreen.Bounds.Width;
int j = Screen.PrimaryScreen.Bounds.Height;
public Form1()
{
InitializeComponent();
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public short dmOrientation;
public short dmPaperSize;
public short dmPaperLength;
public short dmPaperWidth;
public short dmScale;
public short dmCopies;
public short dmDefaultSource;
public short dmPrintQuality;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int ChangeDisplaySettings([In] ref DEVMODE lpDevMode, int dwFlags);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern bool EnumDisplaySettings(string lpszDeviceName, Int32 iModeNum, ref DEVMODE lpDevMode);
void ChangeRes()
{
DEVMODE DevM = new DEVMODE();
DevM.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
bool mybool;
mybool = EnumDisplaySettings(null, 0, ref DevM);
DevM.dmPelsWidth = 1024;//宽
DevM.dmPelsHeight = 768;//高
DevM.dmDisplayFrequency = 60;//刷新频率
DevM.dmBitsPerPel = 32;//颜色象素
long result = ChangeDisplaySettings(ref DevM, 0);
}
void FuYuan()
{
DEVMODE DevM = new DEVMODE();
DevM.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
bool mybool;
mybool = EnumDisplaySettings(null, 0, ref DevM);
DevM.dmPelsWidth = i;//恢复宽
DevM.dmPelsHeight =j;//恢复高
DevM.dmDisplayFrequency = 60;//刷新频率
DevM.dmBitsPerPel = 32;//颜色象素
long result = ChangeDisplaySettings(ref DevM, 0);
}
private void Form1_Load(object sender, EventArgs e)
{
ChangeRes();
}
private void button1_Click(object sender, EventArgs e)
{
FuYuan();
this.Close();
}