Memory hogging screen grabber

brinh123

New member
Joined
Mar 6, 2009
Messages
1
Programming Experience
Beginner
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:
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:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,604
Location
Norway
Programming Experience
10+
Form load: sr.Close()
And why do you check File.Exists for same file twice?

You also have two of these unresolved:
help said:
You should always call the Dispose method to release the Graphics and related resources created by the FromImage method.
You are also creating New Bitmaps to screenGrab variable, Dispose them when you are done with them, ie after DrawImage call. Actually you don't need the screenGrab variable at all.
the memory in use jumps
.Net runtime sorta does that uncontrollably, because it manages the memory of the applications, not only per usage but also per possible allocations and efficiency. So it may not be an indication of a problem, even though in your case it is.

Please put code in code box when posting. It is just one click of a button in the editor.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,604
Location
Norway
Programming Experience
10+
Set the Timers Interval to 1000 ?
 
Top Bottom