Results 1 to 8 of 8

Thread: How to save ImageList

  1. #1

    Thread Starter
    Lively Member cevem's Avatar
    Join Date
    Feb 2010
    Location
    Mars
    Posts
    78

    How to save ImageList

    Hi.

    I'm building a very small and minimalist Movie Collector application. In the Movie Adding dialog, you can select movie's poster. Movie poster is adding to the ImageList.

    Adding Movies



    Movies are listing in the main window as ListView thumbs (large view). First time is no problem;

    No Problem after the adding



    ...but restarting program, no thumbs in the items.

    Restart Application



    Andy idea? Please help me, thanks everybody.

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: How to save ImageList

    How are you saving the data? What is your database?

  3. #3

    Thread Starter
    Lively Member cevem's Avatar
    Join Date
    Feb 2010
    Location
    Mars
    Posts
    78

    Re: How to save ImageList

    Quote Originally Posted by ForumAccount View Post
    How are you saving the data? What is your database?
    BinaryFormatter

    Code:
     Dim FS As New System.IO.FileStream(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\WatchersList\CommonList.mem", IO.FileMode.Create)
                BinFormatter.Serialize(FS, New ArrayList(MovieList.Items))
                FS.Close()

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: How to save ImageList

    I guess at this point it depends on what you want stored. You could store just the file location of the poster and then at runtime you would load in all the pictures from their file paths. Another option is to save a Byte array that represents the image and then at runtime you recreate the image. There are so many different ways you could go with this, you could even use a SQLite database to save everything. It depends on the scope of your application.

  5. #5
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: How to save ImageList

    You could also make an "Album" class, with all members required such as the cover image, title and associated file(s). Next you can store everything in raw data using the BinaryWriter and/or FileStream.

    Example of an "Image pointer" class, which will hold the information of an image stored in a certain region in a file. (Stored in PNG format)
    Code:
    Public Class ImagePointer
        Sub New(ByVal Path As String, ByVal SourceFile As String, ByVal ImageOffset As Long, ByVal ImageLength As Long)
            Me.Path = Path
            Me.SourceFile = SourceFile
            Me.ImageOffset = ImageOffset
            Me.ImageLength = ImageLength
        End Sub
        Public Path As String = ""
        Public SourceFile As String = ""
        Public ImageOffset As Long = 0
        Public ImageLength As Long = 0
        Public Function GetImage() As Image
            Dim s As New System.IO.FileStream(Me.SourceFile, IO.FileMode.Open)
            Dim imgbytes(Me.ImageLength) As Byte
            s.Position = Me.ImageOffset
            s.Read(imgbytes, 0, Me.ImageLength)
            s.Close()
            Dim ms As New System.IO.MemoryStream(imgbytes, 0, imgbytes.Length)
            ms.Write(imgbytes, 0, imgbytes.Length)
            GetImage = Image.FromStream(ms)
            ms.Close()
        End Function
    End Class
    It seems that you could use the Image.FromStream directly on the filestream, but it could also be that I didn't do that since an image length is used.

    Storing it is somewhat the same:
    Code:
                Me._Image = value
                Dim ms As New System.IO.MemoryStream()
                value.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
                Me.imageBytes = ms.ToArray
                ms.Close()
    Best is to start with the format to use, like a header. Example:
    [totalimagecount] (integer)
    for each image
    * [imagenamelength] (integer)
    * [imagename] (char array)
    * [imageoffset] (long)
    * [imagesize] (long)
    for each image
    * [rawdata] (PNG image)
    This way you can neglect loading the images you don't want to load.

    Note: instead of giving the offset and size of each image, you could also send the header length plus the individual sizes of each image. Could save you a few hundred bytes.

  6. #6

    Thread Starter
    Lively Member cevem's Avatar
    Join Date
    Feb 2010
    Location
    Mars
    Posts
    78

    Re: How to save ImageList

    Quote Originally Posted by ForumAccount View Post
    I guess at this point it depends on what you want stored. You could store just the file location of the poster and then at runtime you would load in all the pictures from their file paths. Another option is to save a Byte array that represents the image and then at runtime you recreate the image. There are so many different ways you could go with this, you could even use a SQLite database to save everything. It depends on the scope of your application.
    Thanks to the your reply. Please show a sample for SQL, or link? Thanks again.

  7. #7

    Thread Starter
    Lively Member cevem's Avatar
    Join Date
    Feb 2010
    Location
    Mars
    Posts
    78

    Re: How to save ImageList

    Quote Originally Posted by bergerkiller View Post
    You could also make an "Album" class, with all members required such as the cover image, title and associated file(s). Next you can store everything in raw data using the BinaryWriter and/or FileStream.

    Example of an "Image pointer" class, which will hold the information of an image stored in a certain region in a file. (Stored in PNG format)
    Code:
    Public Class ImagePointer
        Sub New(ByVal Path As String, ByVal SourceFile As String, ByVal ImageOffset As Long, ByVal ImageLength As Long)
            Me.Path = Path
            Me.SourceFile = SourceFile
            Me.ImageOffset = ImageOffset
            Me.ImageLength = ImageLength
        End Sub
        Public Path As String = ""
        Public SourceFile As String = ""
        Public ImageOffset As Long = 0
        Public ImageLength As Long = 0
        Public Function GetImage() As Image
            Dim s As New System.IO.FileStream(Me.SourceFile, IO.FileMode.Open)
            Dim imgbytes(Me.ImageLength) As Byte
            s.Position = Me.ImageOffset
            s.Read(imgbytes, 0, Me.ImageLength)
            s.Close()
            Dim ms As New System.IO.MemoryStream(imgbytes, 0, imgbytes.Length)
            ms.Write(imgbytes, 0, imgbytes.Length)
            GetImage = Image.FromStream(ms)
            ms.Close()
        End Function
    End Class
    It seems that you could use the Image.FromStream directly on the filestream, but it could also be that I didn't do that since an image length is used.

    Storing it is somewhat the same:
    Code:
                Me._Image = value
                Dim ms As New System.IO.MemoryStream()
                value.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
                Me.imageBytes = ms.ToArray
                ms.Close()
    Best is to start with the format to use, like a header. Example:
    Thanks for the reply. This is a good code. How to apply this code on ListView?

  8. #8
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: How to save ImageList

    Well first of all you need public members in the background, which store the information you need:
    Code:
    Public albums As New List(Of Album)
    Public Class Album
        Public title As String
        Public files() As String
        Public cover As Image
    End Class
    When populating your listview, you need to use the imagelist. So before you populate the listview, you populate the imagelist with the images you want to display.
    Code:
    ImageList1.Images.Clear()
    ListView1.Items.Clear()
    For i As Integer = 0 To currentalbums.Count - 1
        ImageList1.Images.Add(currentalbums(i).cover)
        Dim item As New ListViewItem(currentalbums(i).title)
        item.Tag = currentalbums(i).files
        item.ImageIndex = i
        ListView1.Items.Add(item)
    Next
    The way you do this is up to you, obviously. You can add functions to load/save information from a stream in your Album class:
    Code:
    Public Sub LoadFrom(ByVal s As IO.Stream, ByVal coversize As Long)
        Dim b As New IO.BinaryReader(s)
        Me.title = b.ReadString()
        ReDim Me.files(b.ReadInt32() - 1)
        For i As Integer = 0 To Me.files.Count - 1
            Me.files(i) = b.ReadString()
        Next
        b.Close()
        Dim imgbytes(coversize - 1) As Byte
        s.Read(imgbytes, 0, coversize)
        s.Close()
        Dim ms As New System.IO.MemoryStream(imgbytes, 0, imgbytes.Length)
        ms.Write(imgbytes, 0, imgbytes.Length)
        Me.cover = Image.FromStream(ms)
        ms.Close()
    End Sub
    Wrote this from scratch, so it could contain some bugs. Idea is to link everything together, and store certain information globally.
    Try to Dispose as much as possible, storing lots of Image objects is never a good idea.

    EDIT

    I recommend adding a loader class, a class which loads Album information from a source file based on an identifier, like a name. It would make everything a bit easier.
    Public Function AlbumLoader.GetAlbum("Album name") As Album

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width