Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 523
  • Last Modified:

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
0
NCSO
Asked:
NCSO
  • 6
  • 5
1 Solution
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now