How to load graphics fast...

Howdy,

I am playing around with a photoalbum application and most of my pictures are 4MB or larger and loading them into a picture box takes forever.

This is a two part question:

1. How do I load a picture fast into a thumbnail size?
2. How do I keep the aspect ratio on the picture?

I can split it out into two questions if you want, the first question is the most important.

So there...

I load the picture like this:

PictureBox1.Image = Image.FromFile(pics(0))

Thanks
~j
NCSOAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

melmersCommented:
At first, you can convert your image to a lower resolution and file size.
I assume that you use bmp, when you conver them to jpeg then you can load them
faster

Dim myImage as Image = Image.GetFromFile("PathToFile")
myImage.Save("PathToConvertedFile.jpg",ImageFormat.Jpeg)



0
NCSOAuthor Commented:
No 99% is jpeg's so they're already compressed.
0
melmersCommented:
how many pictures you have to display.
is it a solution to preload the pictures into the memory on startup of your application or are that too many pic's?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

NCSOAuthor Commented:
It's everything from 1 picture to maybe 100.

However, I am only showing 12 at a time.   12 thumbnails, and the one selected is shown in a larger picturebox.

I thought of resizing the picture on the fly and it works great but takes time too.

I was just wondering if there was a way to read the picture in a lower resolution or something... this might sound 'tardet...

Preloading is fine, how do I preload a directory full of pictures?

Thank you
~j
0
melmersCommented:
I wouldn't preload the directory because of the huge memory consumtion. load 12 pictures on app start then show them, after showing load the next 12 into a hashtable or something else for the next site. when the user click on the next site show the 12 from the hashtable and load the next 12 into the hashtable.
0
NCSOAuthor Commented:
Any chance of showing me the code loading pictures into a hashtable?

tnx
~j
0
melmersCommented:
Are the Files indexed by a number or are they all with different names?
0
NCSOAuthor Commented:
They are for the most part file-names, like they would be from a digital camera DCN0234.jpg, DCN0235.jpg etc.

The scanned in pictures would typically have a name like Denmark1.jpg, Denmark2.jpg etc.
0
melmersCommented:
Sorry for late response. The code of an example class is
provided. you must only call the class with the the path parameter in the new argument.
then you can go with next and back through the collection and the property GetPictures
gives you an Arraylist with 12 Pictures. The Pictures will never deleted from the memory so
you must see if you must change the code to hold only a few pictures in memory.
Imports System.IO
Public Class PictureLoader
 
    Private myPicFileNames As New ArrayList
    Private myCurrentIndex As Integer = 0
    Private myPicCache As New ArrayList
    Dim maxPicLoadAtOnce As Integer = 12
    Sub New(ByVal Path As String)
        If Not Path.EndsWith("\") Then
            Path &= "\"
        End If
        If Directory.Exists(Path) Then
            Dim myFiles() As String = Directory.GetFiles(Path & "*.jpg")
            For Each myFile As String In myFiles
                myPicFileNames.Add(myFile)
            Next
        End If
 
        If myPicFileNames.Count <= 12 Then
            maxPicLoadAtOnce = myPicFileNames.Count
        End If
 
        For i = 0 To maxPicLoadAtOnce
            Dim tmpImage As New Bitmap(myPicFileNames(i).ToString)
            myPicCache.Add(tmpImage)
 
        Next
    End Sub
 
    Public ReadOnly Property GetPictures() As ArrayList
        Get
            Return myPicCache.GetRange(myCurrentIndex, 12)
        End Get
    End Property
 
    Public Sub [Next]()
        If (myCurrentIndex + 12) <= myPicCache.Count Then
            myCurrentIndex += 12
        Else
            If (myCurrentIndex + 12) <= myPicFileNames.Count Then
                For i = myCurrentIndex To (myCurrentIndex + 12)
                    Dim tmpImage As New Bitmap(myPicFileNames(i).ToString)
                    myPicCache.Add(tmpImage)
                Next
            End If
        End If
    End Sub
 
    Public Sub Back()
        If myCurrentIndex <= 0 And myCurrentIndex <= 12 Then
            myCurrentIndex -= 12
        End If
    End Sub
 
End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NCSOAuthor Commented:
I apologize then for this late responce...

Awesome work, this work perfectly, thanks you so much, and happy New Year!

~J
0
NCSOAuthor Commented:
One question (if you're still watching it)

When I call Next or Back it gives me an error because PicCache is empty, how do I preserve it?

tnx
~j
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.