c#在winfrom里面怎么设置按键录制视频全屏
时间: 2024-03-01 21:50:09 浏览: 71
你可以使用C#中的Screen类来截取屏幕,然后使用AVIFile库将截取的屏幕帧保存为AVI格式的视频文件。以下是一个简单的示例代码:
```csharp
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace ScreenRecorder
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("avifil32.dll")]
static extern void AVIFileInit();
[DllImport("avifil32.dll")]
static extern void AVIFileExit();
[DllImport("avifil32.dll")]
static extern int AVIFileOpenW(ref IntPtr ppfile, [MarshalAs(UnmanagedType.LPWStr)]string szFile, int uMode, int pclsidHandler);
[DllImport("avifil32.dll")]
static extern int AVIFileCreateStream(IntPtr pfile, out IntPtr ppavi, ref AVISTREAMINFOW psi);
[DllImport("avifil32.dll")]
static extern int AVIStreamSetFormat(IntPtr avi, int lPos, ref BITMAPINFOHEADER lpFormat, int cbFormat);
[DllImport("avifil32.dll")]
static extern int AVIStreamWrite(IntPtr pavi, int lStart, int lSamples, IntPtr lpBuffer, int cbBuffer, int dwFlags, int dummy1, int dummy2);
[DllImport("avifil32.dll")]
static extern int AVIStreamRelease(IntPtr pavi);
[DllImport("avifil32.dll")]
static extern int AVIFileRelease(IntPtr pfile);
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct BITMAPINFOHEADER
{
public int biSize;
public int biWidth;
public int biHeight;
public short biPlanes;
public short biBitCount;
public int biCompression;
public int biSizeImage;
public int biXPelsPerMeter;
public int biYPelsPerMeter;
public int biClrUsed;
public int biClrImportant;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct AVISTREAMINFOW
{
public int fccType;
public int fccHandler;
public int dwFlags;
public int dwCaps;
public short wPriority;
public short wLanguage;
public int dwScale;
public int dwRate;
public int dwStart;
public int dwLength;
public int dwInitialFrames;
public int dwSuggestedBufferSize;
public int dwQuality;
public int dwSampleSize;
public short left;
public short top;
public short right;
public short bottom;
}
IntPtr aviFile = IntPtr.Zero;
IntPtr aviStream = IntPtr.Zero;
BITMAPINFOHEADER bih = new BITMAPINFOHEADER();
public Form1()
{
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e)
{
AVIFileInit();
// 打开AVI文件
AVIFileOpenW(ref aviFile, "test.avi", 4097, 0);
// 设置流格式
bih.biSize = Marshal.SizeOf(typeof(BITMAPINFOHEADER));
bih.biWidth = Screen.PrimaryScreen.Bounds.Width;
bih.biHeight = Screen.PrimaryScreen.Bounds.Height;
bih.biPlanes = 1;
bih.biBitCount = 24;
bih.biCompression = 0;
bih.biSizeImage = bih.biWidth * bih.biHeight * 3;
AVISTREAMINFOW strhdr = new AVISTREAMINFOW();
strhdr.fccType = 1935960438;
strhdr.fccHandler = 0;
strhdr.dwFlags = 0;
strhdr.dwCaps = 0;
strhdr.wPriority = 0;
strhdr.wLanguage = 0;
strhdr.dwScale = 1;
strhdr.dwRate = 25;
strhdr.dwStart = 0;
strhdr.dwLength = 0;
strhdr.dwInitialFrames = 0;
strhdr.dwSuggestedBufferSize = bih.biSizeImage;
strhdr.dwQuality = -1;
strhdr.dwSampleSize = 0;
strhdr.left = 0;
strhdr.top = 0;
strhdr.right = (short)bih.biWidth;
strhdr.bottom = (short)bih.biHeight;
AVIFileCreateStream(aviFile, out aviStream, ref strhdr);
AVIStreamSetFormat(aviStream, 0, ref bih, bih.biSize);
timer1.Start();
}
private void btnStop_Click(object sender, EventArgs e)
{
timer1.Stop();
AVIStreamRelease(aviStream);
AVIFileRelease(aviFile);
AVIFileExit();
}
private void timer1_Tick(object sender, EventArgs e)
{
IntPtr hdcSrc = GetWindowDC(GetDesktopWindow());
IntPtr hdcDest = bih.biHeight < 0 ? CreateCompatibleDC(hdcSrc) : CreateCompatibleDC(IntPtr.Zero);
IntPtr hBitmap = CreateCompatibleBitmap(hdcSrc, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
IntPtr hOld = SelectObject(hdcDest, hBitmap);
BitBlt(hdcDest, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, hdcSrc, 0, 0, 13369376);
AVIStreamWrite(aviStream, timer1.Interval * timer1.TickCount, 1, hBitmap, bih.biSizeImage, 0, 0, 0);
SelectObject(hdcDest, hOld);
DeleteObject(hBitmap);
DeleteDC(hdcDest);
ReleaseDC(IntPtr.Zero, hdcSrc);
}
}
}
```
在上面的代码中,我们使用了Windows API函数来截取屏幕,然后使用AVIFile库将帧写入AVI文件。你可以修改代码来控制录制的区域、帧率和保存的文件名等参数。
阅读全文