Is there a way to make a screen recorder in C#? If so, does anybody know of any tutorials I could use or any information on the subject?
+3
A:
Take a look at this VB.NET code.
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
2009-09-29 03:05:36
Stupid freaking WMD bug! Freaking Red! Freaking!!!!!
Lucas McCoy
2009-09-29 03:13:04
The problem was the forward slashes in the quote. I don't know a VB.NET equivalent that won't confuse the editor, so I leave that to you.
John Saunders
2009-09-29 03:17:23
I wrote something very similar in C# are few years back. The performance was horrible. I would be interested in how this performs.
Jason Jackson
2009-09-29 03:20:06
Thread.Sleep() alert!
MusiGenesis
2009-09-29 03:35:23
@Jason Jackson - Any chance I would be able to take a look at the code just to get an idea?
Nate Shoffner
2009-09-29 04:28:11
I don't think I have the code any longer. I wrote it for an employer about 6 years ago. I am sure it is still sitting in their CVS repository but those bits are probably long gone off any drive of mine. Sorry. It did follow the same basic concept: periodic screen shot saved to disk. I remember trying a few different ways of doing it trying to optimize it, but don't recall the details. Too much code written between then and now.
Jason Jackson
2009-09-29 13:01:07
Ok, well I'll use this, converted it to C# (I prefer C# over VB). Hope all goes well. Thanks.
Nate Shoffner
2009-09-29 22:24:09
Maybe you could post the C# version on CodePlex for future use. Good Luck!
Lucas McCoy
2009-09-29 22:34:57