VB.netでListViewに約4100枚のjpg画像ファイルを読み込んでサムネイルで表示しているのですが、ボタンを押してサムネイルが表示されるまで約6分間かかります。これをもっと早く読み込めるように改良したいのですがどうすればいいのかわかりません。画質は大幅に下がっても構いません。
使用しているパソコンのスペック
使用OS Windows10
メモリ 8GB
CPU Intel Core i5-3230M 2.60GHz
処理の実行中は実行しているプロセスのCPU使用率が35パーセントぐらいになります。
ここを参考にしました
VB
1Public Class Form1 2 3 ' 幅w、高さhのImageオブジェクトを作成 4 Function createThumbnail(ByVal image As Image, ByVal w As Integer, ByVal h As Integer) As Image 5 Dim canvas As New Bitmap(w, h) 6 7 Dim g As Graphics = Graphics.FromImage(canvas) 8 g.FillRectangle(New SolidBrush(Color.White), 0, 0, w, h) 9 10 Dim fw As Double = CDbl(w) / CDbl(image.Width) 11 Dim fh As Double = CDbl(h) / CDbl(image.Height) 12 Dim scale As Double = Math.Min(fw, fh) 13 14 Dim w2 As Integer = CInt(image.Width * scale) 15 Dim h2 As Integer = CInt(image.Height * scale) 16 17 g.DrawImage(image, (w - w2) \ 2, (h - h2) \ 2, w2, h2) 18 g.Dispose() 19 20 Return canvas 21 End Function 22 23 ' ButtonのClickイベントのハンドラ 24 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 25 26 Dim imageDir As String = "C:\Users\user\Pictures" ' 画像ディレクトリ 27 Dim jpgFiles As String() = System.IO.Directory.GetFiles(imageDir, "*.jpg") 28 29 Dim width As Integer = 100 30 Dim height As Integer = 80 31 32 ImageList1.ImageSize = New Size(width, height) 33 ListView1.LargeImageList = ImageList1 34 35 Dim original As Image 36 For i As Integer = 0 To jpgFiles.Length - 1 37 38 Try 39 original = Bitmap.FromFile(jpgFiles(i)) 40 Catch 41 original = My.Resources.Resource1._error 42 End Try 43 44 Dim thumbnail As Image = createThumbnail(original, width, height) 45 46 ImageList1.Images.Add(thumbnail) 47 ListView1.Items.Add(jpgFiles(i), i) 48 49 original.Dispose() 50 thumbnail.Dispose() 51 Next 52 End Sub 53 54End Class
お手数をおかけしますがよろしくお願いいたします。
追記
コードの修正によって少し早くなったのでそれを追記します。
以下のコードでほぼ2分、メモリ使用量は250メガバイトほどに短縮できました。
VB
1Public Class Form1 2 3 ' 幅w、高さhのImageオブジェクトを作成 4 Function createThumbnail(ByVal image As Image, ByVal w As Integer, ByVal h As Integer) As Image 5 Dim canvas As New Bitmap(w, h) 6 7 Dim g As Graphics = Graphics.FromImage(canvas) 8 g.FillRectangle(New SolidBrush(Color.White), 0, 0, w, h) 9 10 Dim fw As Double = CDbl(w) / CDbl(image.Width) 11 Dim fh As Double = CDbl(h) / CDbl(image.Height) 12 Dim scale As Double = Math.Min(fw, fh) 13 14 Dim w2 As Integer = CInt(image.Width * scale) 15 Dim h2 As Integer = CInt(image.Height * scale) 16 17 g.DrawImage(image, (w - w2) \ 2, (h - h2) \ 2, w2, h2) 18 g.Dispose() 19 20 Return canvas 21 End Function 22 23 ' ButtonのClickイベントのハンドラ 24 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 25 Dim cnt As Integer = 0 26 27 Dim imageDir As String = "C:\Users\user\Pictures" ' 画像ディレクトリ 28 Dim jpgFiles As String() = System.IO.Directory.GetFiles(imageDir, "*.jpg") 29 30 Dim width As Integer = 100 31 Dim height As Integer = 80 32 33 ImageList1.ImageSize = New Size(width, height) 34 ListView1.LargeImageList = ImageList1 35 36 Dim thumbnail(jpgFiles.Length - 1) As Image 37 38 Dim original As Image 39 For i As Integer = 0 To jpgFiles.Length - 1 '①のブレイクポイントを置く 40 Try 41 original = Bitmap.FromFile(jpgFiles(i)) 42 Catch 43 original = My.Resources.Resource1._error 44 End Try 45 thumbnail(i) = createThumbnail(original, width, height) 46 original.Dispose() 47 Next 48 49 ImageList1.Images.AddRange(thumbnail) '②のブレイクポイントを置く 50 51 For i As Integer = 0 To jpgFiles.Length - 1 52 ListView1.Items.Add(jpgFiles(i), i) 53 Next 54 End Sub 55 56End Class
上記のコードでブレイクポイントを
①For i As Integer = 0 To jpgFiles.Length - 1
の部分と
②ImageList1.Images.AddRange(thumbnail)
の部分に置いてみて時間を計測したところ、Button1を押してから①の部分までで約1秒かかりました。
次に①から②の所までは1分38秒かかりました。次に②から最後まで走らせたら22秒かかりました。
全体で約2分かかってますが、①から②までのサムネイルを作成する処理に時間がかかっているようです。
作ったサムネイルをImageList1に追加するのとListView1.Items.Addの部分で22秒なので、サムネイルを作成する部分をもっと改善出来れば早くなると思います。
それと作っている物ですが
「Visual Studio 2017」でプロジェクトの作成は「Visual Basic」の「Windowsフォームアプリケーション(.Net Framework)」を選択してプロジェクトを作成しています。どのような目的の物を作るかというと、jpg画像ファイルビューワーです。まずディレクトリを選択してそこにある画像ファイルの一覧をサムネイルで表示して、サムネイルのどれかをダブルクリックすると別ウィンドウでその画像が拡大表示されるといったものです。




回答4件
あなたの回答
tips
プレビュー