[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.0

Help speeding up thumbnail listview

Asked by MrManderson in Microsoft Visual Basic.Net, .NET, Visual Basic Programming

Hi Fellow EE'rs

I am using a solution provided by Idle_Mind on a PAQ'd question, however although it works as an example I need some help optimising the code for performance.

I have folders with anywere from 100-1000 high resolution images 300-800 kb in size. I need to show these images in a thumbnail format so have been looking at using GDI+ to resize these images to proportion but it can take about 30-60 secs to load all the images into the view and uses up quite a bit of system memory due to storing all these thumbs in an image list.

My Question is.. What can I do to the following code to speed up the image browsing process as much as possible?

Ie..
How could I stream the thumbnails directly to the listview without having to store them first in an image list?
Could I double buffer the control such that the images load on the fly instead of waiting for all the images int he folder to render first, similar to the way that windows explorer works.

I am using the following function to resize the images

Module THUMB
    ''' <summary>
    ''' Returns a new bitmap resized to fix a maximum size
    ''' while still maintaining the correct aspect ratio.
    ''' </summary>
    ''' <param name="oldImage">The image to create a thumbnail from.</param>
    ''' <param name="maxWidth">The maximum allowed thumbnail width in pixels.</param>
    ''' <param name="maxHeight">The maximum allowed thumbnail height in pixels.</param>
    ''' <returns>A properly scaled thumbnail image.</returns>
    Public Function GetResizedImage(ByRef oldImage As Bitmap, ByVal maxWidth As Integer, ByVal maxHeight As Integer) As Bitmap
        Dim newImage As Bitmap = Nothing

        ' if the image is too large (i.e. needs to be resized).
        If oldImage.Width > maxWidth Or oldImage.Height > maxHeight Then

            ' we use ratios to maintain the proper aspect ratio of the image.
            Dim imgRatio As Decimal = CDec(oldImage.Width) / CDec(oldImage.Height)
            Dim maxRatio As Decimal = CDec(maxWidth) / CDec(maxHeight)

            ' if the image can be scaled down to perfectly fit the max height and max width.
            If imgRatio = maxRatio Then
                newImage = CType(oldImage.GetThumbnailImage(maxWidth, maxHeight, Nothing, New IntPtr()), Bitmap)
            ElseIf imgRatio < maxRatio Then
                ' Adjust the width to match the maximum height.
                Dim newRatio As Decimal = CDec(maxHeight) / CDec(oldImage.Height)
                Dim newWidth As Integer = CType(Decimal.Round(newRatio * oldImage.Width), Integer)

                ' if new image is a thumbnail then use the method that produces better thumbnail results.
                If maxWidth <= 150 AndAlso maxHeight <= 150 Then
                    newImage = CType(oldImage.GetThumbnailImage(newWidth, maxHeight, Nothing, New IntPtr()), Bitmap)
                Else
                    newImage = New Bitmap(oldImage, newWidth, maxHeight)
                End If
            Else
                ' Adjust the height to match the maximum width.
                Dim newRatio As Decimal = CDec(maxWidth) / CDec(oldImage.Width)
                Dim newHeight As Integer = CInt(Decimal.Round(newRatio * oldImage.Height))

                ' if new image is a thumbnail then use the method that produces better thumbnail results.
                If maxWidth <= 150 AndAlso maxHeight <= 150 Then
                    newImage = CType(oldImage.GetThumbnailImage(maxWidth, newHeight, Nothing, New IntPtr()), Bitmap)
                Else
                    newImage = New Bitmap(oldImage, maxWidth, newHeight)
                End If
            End If
        Else
            ' return a copy because it's smaller than our max size.
            newImage = New Bitmap(oldImage)
        End If
        Return newImage
    End Function

End Module


Many Thanks
Steve
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
Imports System
Imports System.IO
 
Public Class ImageBrowser
 
    Private thumbWidth As Byte = 150
    Private thumbHeight As Byte = 150
 
    Private Sub ImageBrowser_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        End
    End Sub
 
    Private Sub ImageBrowser_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ProgressHolder.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        RepositoryItemProgressBar1.ProgressViewStyle = DevExpress.XtraEditors.Controls.ProgressViewStyle.Broken
        RepositoryItemProgressBar1.PercentView = True
 
    End Sub
 
 
    Private Sub updateImages(ByVal Folder() As String)
 
        Dim fi As FileInfo
        Dim fileName As String
        Dim thumbEntry As ListViewItem
        Dim imageList1 As ImageList = New ImageList
        Dim p As Integer, i As Integer, numFiles As Integer = Folder.Length
        Dim myCallback As Image.GetThumbnailImageAbort = New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
 
        pnlView.Items.Clear()
        imageList1.ColorDepth = ColorDepth.Depth24Bit
        imageList1.ImageSize = New Size(thumbWidth, thumbHeight)
        pnlView.LargeImageList = imageList1
        pnlView.SmallImageList = imageList1
 
        pnlView.BeginUpdate()
        RepositoryItemProgressBar1.Step = 0
        ProgressHolder.Visibility = True
 
        i = 0
 
        Dim dtA As New DateTime(Now.Year, Now.Month, Now.Day, Now.Hour, Now.Minute, Now.Second)
        Debug.WriteLine(dtA.ToString)
 
        For Each fileName In Folder
 
            fi = New FileInfo(fileName)
 
            If fi.Extension.ToUpper.Equals(".BMP") Or fi.Extension.ToUpper.Equals(".JPG") Or _
               fi.Extension.ToUpper.Equals(".PNG") Or fi.Extension.ToUpper.Equals(".GIF") Then
 
                Try
                    imageList1.Images.Add(GetResizedImage(Image.FromFile(fi.FullName), thumbWidth, thumbHeight))
                    thumbEntry = New ListViewItem
                    thumbEntry.Text = fi.Name
                    thumbEntry.ImageIndex = imageList1.Images.Count - 1
                    pnlView.Items.Add(thumbEntry)
 
                Catch ex As Exception
                    MsgBox(fi.FullName & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Information, "Error Loading Image")
                End Try
 
            End If
 
            i = i + 1
            Files.Caption = "Images: " & i
            p = CInt(i / numFiles * 100)
 
            RepositoryItemProgressBar1.Step = p
 
            Application.DoEvents()
 
        Next fileName
 
        Dim dtB As New DateTime(Now.Year, Now.Month, Now.Day, Now.Hour, Now.Minute, Now.Second)
        Debug.WriteLine(dtB.ToString)
 
        ProgressHolder.Visibility = False
        pnlView.EndUpdate()
 
        Dim ts As TimeSpan = dtB.Subtract(dtA)
        Debug.WriteLine(ts.ToString)
        Debug.WriteLine(ts.TotalMinutes)
 
    End Sub
 
    Private Function ThumbnailCallback() As Boolean
        Return False
    End Function
 
    Private Sub BarButtonItem2_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick
        updateImages(Directory.GetFiles("C:\Users\Jnr\Documents\Azureus Downloads\IS Lumina General Icons\256\256"))
    End Sub
End Class
[+][-]05/17/08 07:05 AM, ID: 21589377Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Microsoft Visual Basic.Net, .NET, Visual Basic Programming
Sign Up Now!
Solution Provided By: RQuadling
Participating Experts: 3
Solution Grade: A
 
[+][-]05/17/08 10:55 AM, ID: 21590226Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05/17/08 10:58 AM, ID: 21590238Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05/17/08 05:57 PM, ID: 21591240Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]05/17/08 06:03 PM, ID: 21591244Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05/17/08 06:05 PM, ID: 21591246Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05/18/08 06:36 AM, ID: 21592614Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]05/18/08 06:46 AM, ID: 21592644Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81 / EE_QW_2_20070628