如何在.NET中编写屏幕录像机?

有没有办法在C#中制作屏幕录像机? 如果是这样,是否有人知道我可以使用的任何教程或有关该主题的任何信息?

看看这个VB.NET代码。

Public Class ScreenRecorder Private Shared tempDir As String = My.Computer.FileSystem.SpecialDirectories.Temp & "snapshot" Private Shared snap As New System.Threading.Thread(AddressOf Snapshot) Private Shared _Bounds As System.Drawing.Rectangle = System.Windows.Forms.Screen.PrimaryScreen.Bounds Public Shared Property Bounds() As System.Drawing.Rectangle Get Return _Bounds End Get Set(ByVal value As System.Drawing.Rectangle) _Bounds = value End Set End Property Private Shared Sub Snapshot() If Not My.Computer.FileSystem.DirectoryExists(tempDir) Then _ My.Computer.FileSystem.CreateDirectory(tempDir) Dim Co As Integer = 0 Do Co += 1 System.Threading.Thread.Sleep(50) Dim X As New System.Drawing.Bitmap(_Bounds.Width, _Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb) Using G = System.Drawing.Graphics.FromImage(X) G.CopyFromScreen(_Bounds.Location, New System.Drawing.Point(), _Bounds.Size) Dim CurBounds As New System.Drawing.Rectangle(System.Windows.Forms.Cursor.Position - Bounds.Location, System.Windows.Forms.Cursor.Current.Size) Forms.Cursors.Default.Draw(G, CurBounds) End Using Dim FS As New IO.FileStream(tempDir & FormatString(Co.ToString, 5, "0"c) & ".png", IO.FileMode.OpenOrCreate) X.Save(FS, System.Drawing.Imaging.ImageFormat.Png) X.Dispose() FS.Close() Loop End Sub Public Shared Sub ClearRecording() If My.Computer.FileSystem.DirectoryExists(tempDir) Then _ My.Computer.FileSystem.DeleteDirectory(tempDir, FileIO.DeleteDirectoryOption.DeleteAllContents) My.Computer.FileSystem.CreateDirectory(tempDir) End Sub Public Shared Sub Save(ByVal Output As String) Dim G As New Windows.Media.Imaging.GifBitmapEncoder Dim X As New List(Of IO.FileStream) For Each Fi As String In My.Computer.FileSystem.GetFiles(tempDir, FileIO.SearchOption.SearchTopLevelOnly, "*.png") Dim TempStream As New IO.FileStream(Fi, IO.FileMode.Open) Dim Frame = Imaging.BitmapFrame.Create(TempStream) X.Add(TempStream) G.Frames.Add(Frame) Next Dim FS As New IO.FileStream(Output, IO.FileMode.OpenOrCreate) G.Save(FS) FS.Close() For Each St As IO.FileStream In X St.Close() Next End Sub Public Shared Sub Start() snap = New System.Threading.Thread(AddressOf Snapshot) snap.Start() End Sub Public Shared Sub [Stop]() snap.Abort() End Sub Private Shared Function FormatString(ByVal S As String, ByVal places As Integer, ByVal character As Char) As String If S.Length >= places Then Return S For X As Integer = S.Length To places S = character & S Next Return S End Function End Class 

我从Lucas McCoy那里获取了VB.net代码并将其转换为C#。 这记录了5秒钟并将gif保存到桌面。 PS:有时当最后一个屏幕截图仍然通过线程中止然后流尝试读取它时,我会收到错误。

代码很慢。

 using System; using System.Collections.Generic; using System.IO; using System.Drawing; using System.Drawing.Imaging; namespace ConsoleApplication1 { public class ScreenRecorder { private static string tempDir = Path.GetTempPath() + "/snapshot/"; private static System.Threading.Thread snap = new System.Threading.Thread(Snapshot); private static System.Drawing.Rectangle _Bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds; public static System.Drawing.Rectangle Bounds { get { return _Bounds; } set { _Bounds = value; } } private static void Snapshot() { if (!Directory.Exists(tempDir)) Directory.CreateDirectory(tempDir); int Co = 0; do { Co += 1; System.Threading.Thread.Sleep(50); System.Drawing.Bitmap X = new System.Drawing.Bitmap(_Bounds.Width, _Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using(System.Drawing.Graphics G = System.Drawing.Graphics.FromImage(X)) { G.CopyFromScreen(_Bounds.Location, new System.Drawing.Point(), _Bounds.Size); System.Drawing.Rectangle CurBounds = new System.Drawing.Rectangle(System.Drawing.Point.Subtract(System.Windows.Forms.Cursor.Position,Bounds.Size), System.Windows.Forms.Cursor.Current.Size); System.Windows.Forms.Cursors.Default.Draw(G, CurBounds); } System.IO.FileStream FS = new System.IO.FileStream(tempDir + FormatString(Co.ToString(), 5, '0') + ".png", System.IO.FileMode.OpenOrCreate); X.Save(FS, System.Drawing.Imaging.ImageFormat.Png); X.Dispose(); FS.Close(); } while (true); } public static void ClearRecording() { if (Directory.Exists(tempDir)) Directory.Delete(tempDir, true); Directory.CreateDirectory(tempDir); } public static void Save(string Output) { System.Windows.Media.Imaging.GifBitmapEncoder G = new System.Windows.Media.Imaging.GifBitmapEncoder(); List X = new List(); foreach (string Fi in Directory.GetFiles(tempDir, "*.png", SearchOption.TopDirectoryOnly)) { System.IO.FileStream TempStream = new System.IO.FileStream(Fi, System.IO.FileMode.Open); System.Windows.Media.Imaging.BitmapFrame Frame = System.Windows.Media.Imaging.BitmapFrame.Create(TempStream); X.Add(TempStream); G.Frames.Add(Frame); } System.IO.FileStream FS = new System.IO.FileStream(Output, System.IO.FileMode.OpenOrCreate); G.Save(FS); FS.Close(); foreach (System.IO.FileStream St in X) { St.Close(); } } public static void Start() { snap = new System.Threading.Thread(Snapshot); snap.Start(); } public static void Stop() { snap.Abort(); } private static string FormatString(string S, int places, char character) { if (S.Length >= places) return S; for (int X = S.Length; X <= places; X++) { S = character + S; } return S; } } class Program { static void Main(string[] args) { ScreenRecorder.Start(); System.Threading.Thread.Sleep(5000); ScreenRecorder.Stop(); ScreenRecorder.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\video.gif"); ScreenRecorder.ClearRecording(); } } } 

这些是我必须添加的参考:

在此处输入图像描述

对于线程exception,您可以使用此代码避免这种情况

  public static void Stop() { flag = false; snap.Join(); } 

并将该标志作为静态私有bool(全局)并将其放在while循环中而不是true值:

 while(flag) 

如何避免线程重叠资源。 我知道这不是主题,但我想分享这条信息,因为线程总是使调试变得讨厌。

此致,法瓦兹