ScreenRecorder in .NET 3.0


Requirements:

* .NET 3.0 installed on the computer.
* WPF Extensions for VS 2005

(Both found on Microsoft.com)

What you need to do:

1. Create a new "Windows Application (WPF)"
img1.jpg
2. Add a reference to System.Drawing.dll, and System.Windows.Forms.dll
3. Add the following code:

VISUALBASIC Code
  1. Public Class ScreenRecorder
  2.  
  3. Private Shared tempDir As String = My.Computer.FileSystem.SpecialDirectories.Temp & "\snapshot\"
  4. Private Shared snap As New System.Threading.Thread(AddressOf Snapshot)
  5. Private Shared _Bounds As System.Drawing.Rectangle = System.Windows.Forms.Screen.PrimaryScreen.Bounds
  6. Public Shared Property Bounds() As System.Drawing.Rectangle
  7. Get
  8. Return _Bounds
  9. End Get
  10. Set(ByVal value As System.Drawing.Rectangle)
  11. _Bounds = value
  12. End Set
  13. End Property
  14. Private Shared Sub Snapshot()
  15. If Not My.Computer.FileSystem.DirectoryExists(tempDir) Then _
  16. My.Computer.FileSystem.CreateDirectory(tempDir)
  17. Dim Co As Integer = 0
  18. Do
  19. Co += 1
  20. System.Threading.Thread.Sleep(50)
  21. Dim X As New System.Drawing.Bitmap(_Bounds.Width, _Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
  22. Using G = System.Drawing.Graphics.FromImage(X)
  23. G.CopyFromScreen(_Bounds.Location, New System.Drawing.Point(), _Bounds.Size)
  24. Dim CurBounds As New System.Drawing.Rectangle(System.Windows.Forms.Cursor.Position - Bounds.Location, System.Windows.Forms.Cursor.Current.Size)
  25. Forms.Cursors.Default.Draw(G, CurBounds)
  26. End Using
  27. Dim FS As New IO.FileStream(tempDir & FormatString(Co.ToString, 5, "0"c) & ".png", IO.FileMode.OpenOrCreate)
  28. X.Save(FS, System.Drawing.Imaging.ImageFormat.Png)
  29. X.Dispose()
  30. FS.Close()
  31. Loop
  32. End Sub
  33. Public Shared Sub ClearRecording()
  34. If My.Computer.FileSystem.DirectoryExists(tempDir) Then _
  35. My.Computer.FileSystem.DeleteDirectory(tempDir, FileIO.DeleteDirectoryOption.DeleteAllContents)
  36. My.Computer.FileSystem.CreateDirectory(tempDir)
  37. End Sub
  38. Public Shared Sub Save(ByVal Output As String)
  39. Dim G As New Windows.Media.Imaging.GifBitmapEncoder
  40.  
  41. Dim X As New List(Of IO.FileStream)
  42. For Each Fi As String In My.Computer.FileSystem.GetFiles(tempDir, FileIO.SearchOption.SearchTopLevelOnly, "*.png")
  43. Dim TempStream As New IO.FileStream(Fi, IO.FileMode.Open)
  44. Dim Frame = Imaging.BitmapFrame.Create(TempStream)
  45. X.Add(TempStream)
  46. G.Frames.Add(Frame)
  47. Next
  48. Dim FS As New IO.FileStream(Output, IO.FileMode.OpenOrCreate)
  49. G.Save(FS)
  50. FS.Close()
  51.  
  52. For Each St As IO.FileStream In X
  53. St.Close()
  54.  
  55. Next
  56.  
  57. End Sub
  58. Public Shared Sub Start()
  59. snap = New System.Threading.Thread(AddressOf Snapshot)
  60. snap.Start()
  61. End Sub
  62. Public Shared Sub [Stop]()
  63. snap.Abort()
  64. End Sub
  65. Private Shared Function FormatString(ByVal S As String, ByVal places As Integer, ByVal character As Char) As String
  66. If S.Length >= places Then Return S
  67. For X As Integer = S.Length To places
  68. S = character & S
  69. Next
  70. Return S
  71. End Function
  72.  
  73. End Class


To use the code:
Starting:
VISUALBASIC Code
  1. ScreenRecorder.Start


Stopping:
VISUALBASIC Code
  1. ScreenRecorder.Stop



Saving:
VISUALBASIC Code
  1.  
  2. ScreenRecorder.Save("")


Setting the recording boundries:
VISUALBASIC Code
  1.  
  2. ScreenRecorder.Bounds = New System.Drawing.Rectangle(300, 300, 500, 300)


Erasing the recording:
VISUALBASIC Code
  1.  
  2. ScreenRecorder.ClearRecording()
ray230's Avatar
Author:
Views:
2,304
Rating:
Posted on Friday 17th October 2008 at 01:16 AM
ShadowMage
ShadowMage's Avatar
I thought you were the "VB King"?
Posted on Wednesday 11th July 2007 at 02:53 PM
vb_king
vb_king's Avatar
O_o too complicated god.