Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15661

VS 2015 vb.net memory goes up when pictures are changing in slide show

$
0
0
if someone could direct me in the right direction because im noticing my memory goes up with time as pictures are changing.

vb.net Code:
  1. Public Class frmSecondaryDisplay
  2.     Private imageFiles() As String = Nothing
  3.  
  4.     ' Image index
  5.     Private selected As Integer = 0
  6.     Private begin As Integer = 0
  7.     Private [end] As Integer = 0
  8.     Private Sub frmSecondaryDisplay_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  9.         Try
  10.             con = New SqlConnection(cs)
  11.             con.Open()
  12.             cmd = New SqlCommand("SELECT RTRIM(CompanyName), RTRIM(Address),RTRIM(City),RTRIM(ContactNo),Logo from Company", con)
  13.             rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
  14.             If rdr.Read Then
  15.                 lblCompanyName.Text = rdr.GetValue(0).ToString()
  16.                 lblAddress1.Text = rdr.GetValue(1).ToString()
  17.                 lblAddress2.Text = rdr.GetValue(2).ToString()
  18.                 lblContactNo.Text = "Tel. : " & rdr.GetValue(3).ToString()
  19.                 Dim data As Byte() = DirectCast(rdr.GetValue(4), Byte())
  20.                 Dim ms As New MemoryStream(data)
  21.                 If Me.PictureBox1.Image IsNot Nothing Then PictureBox1.Dispose()
  22.                 Me.PictureBox1.Image = Image.FromStream(ms)
  23.             End If
  24.             con.Close()
  25.             Me.imageFiles = GetFiles(Application.StartupPath & "\Secondary Display Images",
  26.                                          "*.jpg;*.jpeg;*.png;*.bmp;*.tif;*.tiff;*.gif")
  27.             Me.selected = 0
  28.             Me.begin = 0
  29.             Me.end = imageFiles.Length
  30.             ShowImage(imageFiles(selected), pbSlideShow)
  31.         Catch ex As Exception
  32.             MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  33.         End Try
  34.     End Sub
  35.  
  36.     Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs)
  37.         Close()
  38.     End Sub
  39.     Public Shared Sub ShowImage(ByVal path As String, ByVal pct As PictureBox)
  40.         pct.ImageLocation = path
  41.     End Sub
  42.     Public Shared Function GetFiles(ByVal path As String, ByVal searchPattern As String) As String()
  43.         Dim patterns() As String = searchPattern.Split(";"c)
  44.         Dim files As New List(Of String)()
  45.         For Each filter As String In patterns
  46.             ' Iterate through the directory tree and ignore the
  47.             ' DirectoryNotFoundException or UnauthorizedAccessException
  48.             ' exceptions.
  49.             ' [url]http://msdn.microsoft.com/en-us/library/bb513869.aspx[/url]
  50.  
  51.             ' Data structure to hold names of subfolders to be
  52.             ' examined for files.
  53.             Dim dirs As New Stack(Of String)(20)
  54.  
  55.             If Not Directory.Exists(path) Then
  56.                 Throw New ArgumentException()
  57.             End If
  58.             dirs.Push(path)
  59.  
  60.             Do While dirs.Count > 0
  61.                 Dim currentDir As String = dirs.Pop()
  62.                 Dim subDirs() As String
  63.                 Try
  64.                     subDirs = Directory.GetDirectories(currentDir)
  65.                     ' An UnauthorizedAccessException exception will be thrown
  66.                     ' if we do not have discovery permission on a folder or
  67.                     ' file. It may or may not be acceptable to ignore the
  68.                     ' exception and continue enumerating the remaining files
  69.                     ' and folders. It is also possible (but unlikely) that a
  70.                     ' DirectoryNotFound exception will be raised. This will
  71.                     ' happen if currentDir has been deleted by another
  72.                     ' application or thread after our call to Directory.Exists.
  73.                     ' The choice of which exceptions to catch depends entirely
  74.                     ' on the specific task you are intending to perform and
  75.                     ' also on how much you know with certainty about the
  76.                     ' systems on which this code will run.
  77.                 Catch e As UnauthorizedAccessException
  78.                     Continue Do
  79.                 Catch e As DirectoryNotFoundException
  80.                     Continue Do
  81.                 End Try
  82.  
  83.                 Try
  84.                     files.AddRange(Directory.GetFiles(currentDir, filter))
  85.                 Catch e As UnauthorizedAccessException
  86.                     Continue Do
  87.                 Catch e As DirectoryNotFoundException
  88.                     Continue Do
  89.                 End Try
  90.  
  91.                 ' Push the subdirectories onto the stack for traversal.
  92.                 ' This could also be done before handing the files.
  93.                 For Each str As String In subDirs
  94.                     dirs.Push(str)
  95.                 Next str
  96.             Loop
  97.         Next filter
  98.  
  99.         Return files.ToArray()
  100.     End Function
  101.     Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
  102.         ShowNextImage()
  103.     End Sub
  104.     Private Sub ShowNextImage()
  105.         Me.selected += 1
  106.         ShowImage(Me.imageFiles((Me.selected) Mod Me.imageFiles.Length), Me.pbSlideShow)
  107.     End Sub
  108. End Class

Viewing all articles
Browse latest Browse all 15661

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>