I've pieced together a few bits of code, to create an app that takes a screen shot once a minute, and dump it to a jpeg. The code works fine, but for each time the function is called, the memory in use jumps by about 25MB. Can anyone tell me where this is happening and how I can stop it?
Thanks!
Code is:
Thanks!
Code is:
VB.NET:
Imports System
Imports System.Windows.Forms
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing.Imaging
Imports System.Drawing
Imports System.IO
Public Class Form1
Dim timegone As Long
Dim dumppath As String = "c:\windows"
Dim myHost As String = System.Net.Dns.GetHostName
Dim pic As Bitmap
Dim filename As String = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If File.Exists("location.dat") = True Then
Dim sr As StreamReader = File.OpenText("location.dat")
dumppath = sr.ReadLine()
End If
If File.Exists("location.dat") = False Then dumppath = "c:\"
End Sub
Private Function TakeShotOfScreens() As Bitmap
Dim maxHeight As Integer = 0
Dim maxWidth As Integer = 0
For Each scr As Screen In Screen.AllScreens
maxWidth += scr.Bounds.Width
If scr.Bounds.Height > maxHeight Then maxHeight = scr.Bounds.Height
Next
Dim allScreensCapture As New Bitmap(maxWidth, maxHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
Dim screenGrab As Bitmap
Dim screenSize As Size
Dim g As Graphics
Dim g2 As Graphics = Graphics.FromImage(allScreensCapture)
Dim a As New Point(0, 0)
For Each scr As Screen In Screen.AllScreens
screenSize = New Size(scr.Bounds.Width, scr.Bounds.Height)
screenGrab = New Bitmap(scr.Bounds.Width, scr.Bounds.Height)
g = Graphics.FromImage(screenGrab)
g.CopyFromScreen(a, New Point(0, 0), screenSize)
g2.DrawImage(screenGrab, a)
a.X += scr.Bounds.Width
Next
Return allScreensCapture
End Function
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
timegone = timegone + 1
Dim Now As DateTime = DateTime.Now
pic = TakeShotOfScreens()
filename = dumppath & UCase$(myHost) & "-" & (Now.Year) & (Now.Month) & (Now.Day) & "-" & (Now.Hour) & (Now.Minute) & ".jpg"
pic.Save(filename, ImageFormat.Jpeg)
pic.Dispose()
End Sub
End Class
Last edited by a moderator: