8 Replies - 7303 Views - Last Post: 11 October 2012 - 04:10 PM Rate Topic: -----

#1 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 976
  • Joined: 09-May 09

Dispose of picturebox image, dynamic picturbox

Posted 27 August 2012 - 08:42 AM

Hi Guys,

ive created a form that will an unknown amount of populated pictureboxes. I dynamically create as many pictureboxes as i require, problem i have is i need to delete all of the images in a directory (these images populated the pictureboxes).

I have old images from a previous call and i dont want them displayed

    Private Sub posterPicClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)

        'Cast object to get image properties 
        Dim pic As PictureBox = DirectCast(sender, PictureBox)
        Dim posterNo = pic.Name


        Dim i = Form1.ListView20.Items.Item(Form1.ListView20.SelectedIndices(0)).Index
        'copy poster to movie location
        Dim file = "C:\Program Files (x86)\myMovie Manager v3.01\Posters" + "\folder" + posterNo.ToString + ".jpg"



        'Delete old posters before fetching the new ones, dipose of image objects first to 
        'release from memory before deleting
        For Each ctl As Control In Me.Controls
            If (TypeOf ctl Is PictureBox) Then
                Dim picBox As PictureBox = DirectCast(ctl, PictureBox)
                picBox.Image.Dispose()
            End If
        Next ctl


        Dim s As String
        For Each s In System.IO.Directory.GetFiles("C:\Program Files (x86)\myMovie Manager v3.01\Posters")

            Try
                System.IO.File.Delete(s)
            Catch ex As Exception

            End Try

        Next s


        'download HD Movie poster
        Try
            updateHDPoster(Form1.directorylist(i), "\MetaData\movieData.xml", i)
        Catch ex As Exception

        End Try
        Try
            'Copy image
            System.IO.File.Copy(file, Form1.directorylist(i) + "\folder.jpg", True)
            Me.Close()
        Catch ex As Exception

        End Try


    End Sub



Thanks for having a look :)
kevin

Is This A Good Question/Topic? 0
  • +

Replies To: Dispose of picturebox image, dynamic picturbox

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9389
  • View blog
  • Posts: 35,264
  • Joined: 12-June 08

Re: Dispose of picturebox image, dynamic picturbox

Posted 27 August 2012 - 08:48 AM

As an aside - infinite pictureboxes - that sounds like a horrible idea. Why not have a set number of picture boxes and "page through" the next X number or back X number?

As for your dilemma - do one of two things.. either a. kill your collection of pictureboxes and generate them a new with the new graphics (after deleting the old images)... or b. know which image is replacing which one, find that picturebox in your collection, and update it.

The latter will require you to track (perhaps in a custom object?) the image to picturebox to some sort of id so you know what you are updating.
Was This Post Helpful? 0
  • +
  • -

#3 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 976
  • Joined: 09-May 09

Re: Dispose of picturebox image, dynamic picturbox

Posted 28 August 2012 - 01:22 AM

Thanks for the reply but kill is used for processes and for the other idea

i have tried creating a global variable for pictureBoxes then accessing the PB(i).image.dispose() but this fails also.

i have a handle on the click event of the picture box that allows me to access the currently select image but thats it. Ive have little experience with dynamically created controls so my knowledge of finding the best approah to tesolving this issue is limited

As for the unknown about of pictureboxes on a form, each movie.xml file has different amounts of atwork so i cannot set a static value, typically there is no more than 50.

Ive also tried getting all the crontrols of type picturebox on the form but it doesnt see the dynamically created ones.

Any ideas?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9389
  • View blog
  • Posts: 35,264
  • Joined: 12-June 08

Re: Dispose of picturebox image, dynamic picturbox

Posted 28 August 2012 - 07:11 AM

I am not sure what your post is going on about. I am not sure what you are doing when you say you tried ot create a global variable for pictureboxes, nor what you really did when you tried a 'dispose' on them.

Again - pictureboxes, in the hands a novice, are best left as static containers to be filled on the screen, and not treated as transient variables to create or destroy.

What you should be doing is have a grid of picture boxes on the screen and implement a paging system. The app loads up, reads from this XML file, and fills in the pictures as needed. If there are more entries in the XML file than pictureboxes on the screen show a "page right" option that, when clicked, loads up the next set of entries and so on. You know - a basic paging setup. This keeps the picturebox/gui bits stable and the only thing you need to worry about is the entries getting into the right boxes.

As for why the images are not upload - who knows? In the code you have above - have you put a break point on line 10 to see what value "file" is actually being set to?

Also I do not see where you are creating new pictureboxes and assigning them to the me.controls.

Also, why are you not explicitly declaring your datatypes?

Dim posterNo = pic.Name

This is a bad for numerous reasons - the chief one being you may be assuming it is a datatype that it is not.
Was This Post Helpful? 0
  • +
  • -

#5 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 976
  • Joined: 09-May 09

Re: Dispose of picturebox image, dynamic picturbox

Posted 28 August 2012 - 08:30 AM

Hi, thanks for the response, i will post full code tonight and ill looking into the methods you outline. Maybe my logic is wrong but im determined to find the best way.

Thanks for you helps
Was This Post Helpful? 0
  • +
  • -

#6 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 976
  • Joined: 09-May 09

Re: Dispose of picturebox image, dynamic picturbox

Posted 28 August 2012 - 01:11 PM

OK, here is my code, ill try and explain.

The user clicks on a button and i open a new form and display all of the available posters of size thumb,

 Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Me.Cursor = Cursors.WaitCursor

        Try

            'get integer value of selected movie
            Dim i = ListView20.Items.Item(ListView20.SelectedIndices(0)).Index


            'Get Movie Name from selection
            Dim movieName = ListView20.Items.Item(ListView20.SelectedIndices(0)).Text
            Dim directory = directorylist.Item(i)

            'get images 
            PosterViewer2.fetchImages(directory, "\MetaData\movieData.xml", sender, e)

            'display form
            PosterViewer2.Show()

            'dont download same images
            posterIndex = i
        Catch ex As Exception
       
        End Try



        'change HD poster image
        frontCoverPicturebox.Image = Image.FromFile(directorylist.Item(posterIndex) + "\folderHD.jpg")

        'Change poster image in carousel
        Dim selectedButton As RadButtonElement
        selectedButton = CType(RadCarousel1.SelectedItem, RadButtonElement)
        selectedButton.Image = Image.FromFile(directorylist.Item(posterIndex) + "\folder.jpg")
        RadCarousel1.Update()

     
        'change cusor look
        Me.Cursor = Cursors.Arrow
    End Sub




'Fetch Posters method
    Sub fetchImages(ByVal directoryName As String, ByVal xmlFileName As String, ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim i As Integer = 0

        'FilePath
        Dim fileName As String = String.Concat(directoryName, xmlFileName)

        'Get the movie name
        Dim movieTitle() As String
        movieTitle = directoryName.Split("\")


        If File.Exists(fileName) Then

            'New document
            Dim doc As XDocument


            Try

                'load file into document
                doc = Xdocument.Load(fileName)

                'Query for poster data
                Dim moviePoster = From p In doc.Descendants("image")
                                  Where p.Attribute("type") = "poster" And p.Attribute("size") = "thumb"
                                  Select posterURL = p.Attribute("url").Value.ToString



                'download posters and put in temp location
                While i <= moviePoster.Count

                    Try

                        'Download poster
                        Dim temp As String = String.Concat("C:\Program Files (x86)\myMovie Manager v3.01\Posters\folder", i)
                        Dim coverFilePath = String.Concat(temp, ".jpg")
                        Dim webClient As New System.Net.WebClient
                        webClient.DownloadFile(moviePoster(i).ToString, coverFilePath)
                        webClient.Dispose()

                        i = i + 1
                    Catch ex As Exception

                        i = i + 1
                    End Try

                End While

            Catch ex As Exception

            End Try
        End If


        'Get total number of posters in directory
        Dim di As New IO.DirectoryInfo("C:\Program Files (x86)\myMovie Manager v3.01\Posters")
        Dim aryFi As IO.FileInfo() = di.GetFiles("*.jpg")



        'x position
        Dim xPos As Integer = 10

        'Create array for pictureboxes 
        Dim PB(aryFi.Count) As PictureBox

        Dim k As Integer = 0
        While k <= aryFi.Count


            'assign picturebox to array in correct position with name
            PB(k) = New PictureBox
            PB(k).Name = k

            'try and load image from file 
            Try

                Dim path = "C:\Program Files (x86)\myMovie Manager v3.01\Posters" + "\folder" + k.ToString + ".jpg"
                If File.Exists(path) Then
                    PB(k).Image = Image.FromFile(path)
                End If

            Catch ex As Exception

            End Try

            'position of the picturebox 
            PB(k).Location = New System.Drawing.Point(xPos, 10)

            PB(k).Size = New Size(92, 138)
            PB(k).SizeMode = PictureBoxSizeMode.StretchImage
            PB(k).BorderStyle = 1

            'Add control to panel 1
            Panel1.Controls.Add(PB(k))

            '  Wire this control up to an appropriate event handler
            AddHandler PB(k).Click, AddressOf posterPicClicked

            xPos = xPos + 97

            k = k + 1
        End While


    End Sub



After the above method has completed all of the posters are now in a temp folder location and
and are displayed on the new form.

this is the code for when the user clicks a poster
    Private Sub posterPicClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)

        'Cast object to get image properties 
        Dim pic As PictureBox = DirectCast(sender, PictureBox)
        'USED TO GET THE NAME OF THE PICTURE CLICKED
        Dim posterNo = pic.Name



        'index of selected item in listview
        Dim i = Form1.ListView20.Items.Item(Form1.ListView20.SelectedIndices(0)).Index

        'download HD Movie poster
        Try
            updateHDPoster(Form1.directorylist(i), "\MetaData\movieData.xml", i)
        Catch ex As Exception

        End Try



        Try
            'Create path for the selected poster 
            Dim file = "C:\Program Files (x86)\myMovie Manager v3.01\Posters" + "\folder" + posterNo.ToString + ".jpg"

            'Copy image - thumb
            System.IO.File.Copy(File, Form1.directorylist(i) + "\folder.jpg", True)

        Catch ex As Exception

        End Try

        'ERROR HERE, HANDLE ON IMAGE 
        'delete each poster the temp folder location
        Dim poster As String = Nothing
        For Each poster In System.IO.Directory.GetFiles("C:\Program Files (x86)\myMovie Manager v3.01\Posters")

            Try
                'remove poster
                File.Delete(poster)
            Catch ex As Exception

            End Try

        Next poster

        'Close form
        Me.Close()
    End Sub



kevin
Was This Post Helpful? 0
  • +
  • -

#7 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 976
  • Joined: 09-May 09

Re: Dispose of picturebox image, dynamic picturbox

Posted 28 August 2012 - 01:39 PM

Solved . . .


Changed this
'try and load image from file 
            Try

                Dim path = "C:\Program Files (x86)\myMovie Manager v3.01\Posters" + "\folder" + k.ToString + ".jpg"
                If File.Exists(path) Then
                    PB(k).image = image.fromfile(path)
                End If

            Catch ex As Exception

            End Try



To This
'try and load image from file 
            Try

                Dim path = "C:\Program Files (x86)\myMovie Manager v3.01\Posters" + "\folder" + k.ToString + ".jpg"
                If File.Exists(path) Then
                    PB(k).Load(path)
                End If

            Catch ex As Exception

            End Try



i can now delete the posters in the temp folder location
Was This Post Helpful? 0
  • +
  • -

#8 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 976
  • Joined: 09-May 09

Re: Dispose of picturebox image, dynamic picturbox

Posted 11 October 2012 - 04:04 PM

SOLVED . . ..

handle was held on the picturebox array, dispose of pictureboxes in array

Dim P As Integer = 0
                While P < PB.Length - 1
                    PB(P).Dispose()
                    P = P + 1
                End While




thanks to all who looked.
Was This Post Helpful? 0
  • +
  • -

#9 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 976
  • Joined: 09-May 09

Re: Dispose of picturebox image, dynamic picturbox

Posted 11 October 2012 - 04:10 PM

oops
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1