How to load graphics fast...


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))

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.

melmersSenior Software Deveolper/ArchitectCommented:
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

Dim myImage as Image = Image.GetFromFile("PathToFile")

NCSOAuthor Commented:
No 99% is jpeg's so they're already compressed.
melmersSenior Software Deveolper/ArchitectCommented:
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?
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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
melmersSenior Software Deveolper/ArchitectCommented:
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.
NCSOAuthor Commented:
Any chance of showing me the code loading pictures into a hashtable?

melmersSenior Software Deveolper/ArchitectCommented:
Are the Files indexed by a number or are they all with different names?
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.
melmersSenior Software Deveolper/ArchitectCommented:
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
        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)
    End Sub
    Public ReadOnly Property GetPictures() As ArrayList
            Return myPicCache.GetRange(myCurrentIndex, 12)
        End Get
    End Property
    Public Sub [Next]()
        If (myCurrentIndex + 12) <= myPicCache.Count Then
            myCurrentIndex += 12
            If (myCurrentIndex + 12) <= myPicFileNames.Count Then
                For i = myCurrentIndex To (myCurrentIndex + 12)
                    Dim tmpImage As New Bitmap(myPicFileNames(i).ToString)
            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

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!

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?

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.