|
-
Jun 27th, 2011, 09:55 AM
#1
Thread Starter
Lively Member
-
Jun 27th, 2011, 09:57 AM
#2
Re: How to save ImageList
How are you saving the data? What is your database?
-
Jun 27th, 2011, 10:00 AM
#3
Thread Starter
Lively Member
Re: How to save ImageList
 Originally Posted by ForumAccount
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()
-
Jun 27th, 2011, 10:08 AM
#4
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.
-
Jun 27th, 2011, 10:14 AM
#5
Fanatic Member
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.
Last edited by bergerkiller; Jun 27th, 2011 at 10:27 AM.
-
Jun 27th, 2011, 10:17 AM
#6
Thread Starter
Lively Member
Re: How to save ImageList
 Originally Posted by ForumAccount
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.
-
Jun 27th, 2011, 10:27 AM
#7
Thread Starter
Lively Member
Re: How to save ImageList
 Originally Posted by bergerkiller
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?
-
Jun 27th, 2011, 10:40 AM
#8
Fanatic Member
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
Last edited by bergerkiller; Jun 27th, 2011 at 10:45 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|