回答編集履歴
1
追記
answer
CHANGED
@@ -2,4 +2,143 @@
|
|
2
2
|
ファイル名のみとしてListViewに追加。
|
3
3
|
サムネイルは別スレッドでキャッシュを作成しつつ、ListViewへの描画はOwnerDrawにてアイテムを描画する時にキャッシュにあればキャッシュから、キャッシュにない場合はその場でサムネイル作成→キャッシュへ追加→描画
|
4
4
|
とすれば高速化できないでしょうか。
|
5
|
-
ちょっと古い記事ですが[サムネイル画像の生成と表示](https://clown.cube-soft.jp/entry/20101008/1286517159)が参考になるのではないかと思います。
|
5
|
+
ちょっと古い記事ですが[サムネイル画像の生成と表示](https://clown.cube-soft.jp/entry/20101008/1286517159)が参考になるのではないかと思います。
|
6
|
+
|
7
|
+
とりあえず動くものを作ってみました。
|
8
|
+
言いたいことは伝わるのではないかと思います。
|
9
|
+
```VBNET
|
10
|
+
Imports System.Threading
|
11
|
+
|
12
|
+
Public Class Form1
|
13
|
+
|
14
|
+
Const thumbnailwidth As Integer = 100
|
15
|
+
Const thumbnailheight As Integer = 80
|
16
|
+
|
17
|
+
Private listitem As New List(Of ListViewItem)
|
18
|
+
Private thumbnailCache As New Dictionary(Of String, Image)
|
19
|
+
Private listlock As New Object
|
20
|
+
Private cts As CancellationTokenSource
|
21
|
+
Private cachetask As Task
|
22
|
+
|
23
|
+
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
|
24
|
+
Dim imagelist1 As New ImageList
|
25
|
+
imagelist1.ImageSize = New Size(thumbnailwidth, thumbnailheight)
|
26
|
+
ListView1.LargeImageList = imagelist1
|
27
|
+
ListView1.OwnerDraw = True
|
28
|
+
End Sub
|
29
|
+
|
30
|
+
|
31
|
+
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
|
32
|
+
Dim imageDir As String = "C:\Users\user\Pictures" ' 画像ディレクトリ
|
33
|
+
Dim jpgFiles As IEnumerable = System.IO.Directory.EnumerateFiles(imageDir, "*.jpg", IO.SearchOption.AllDirectories)
|
34
|
+
|
35
|
+
'サムネイルキャッシュ作成中であればキャンセル
|
36
|
+
If cachetask?.Status = TaskStatus.Running Then
|
37
|
+
cts.Cancel()
|
38
|
+
cachetask.Wait()
|
39
|
+
End If
|
40
|
+
|
41
|
+
listitem.Clear()
|
42
|
+
thumbnailCache.Clear()
|
43
|
+
|
44
|
+
For Each file As String In jpgFiles
|
45
|
+
Dim item = New ListViewItem(file)
|
46
|
+
listitem.Add(item)
|
47
|
+
Next
|
48
|
+
ListView1.VirtualMode = True
|
49
|
+
ListView1.VirtualListSize = listitem.Count
|
50
|
+
|
51
|
+
'キャッシュ作成開始
|
52
|
+
CreateCache()
|
53
|
+
End Sub
|
54
|
+
|
55
|
+
Private Sub ListView1_RetrieveVirtualItem(sender As Object, e As RetrieveVirtualItemEventArgs) Handles ListView1.RetrieveVirtualItem
|
56
|
+
If e.Item Is Nothing Then e.Item = listitem(e.ItemIndex)
|
57
|
+
End Sub
|
58
|
+
|
59
|
+
|
60
|
+
Function createThumbnail(filename As String) As Image
|
61
|
+
Dim canvas As New Bitmap(thumbnailwidth, thumbnailheight)
|
62
|
+
|
63
|
+
Using original = Bitmap.FromFile(filename)
|
64
|
+
|
65
|
+
Using g As Graphics = Graphics.FromImage(canvas)
|
66
|
+
Using WhiteBrush = New SolidBrush(Color.White)
|
67
|
+
g.FillRectangle(WhiteBrush, 0, 0, thumbnailwidth, thumbnailheight)
|
68
|
+
End Using
|
69
|
+
|
70
|
+
Dim fw As Double = CDbl(thumbnailwidth) / CDbl(original.Width)
|
71
|
+
Dim fh As Double = CDbl(thumbnailheight) / CDbl(original.Height)
|
72
|
+
Dim scale As Double = Math.Min(fw, fh)
|
73
|
+
|
74
|
+
Dim w2 As Integer = CInt(original.Width * scale)
|
75
|
+
Dim h2 As Integer = CInt(original.Height * scale)
|
76
|
+
|
77
|
+
g.DrawImage(original, (thumbnailwidth - w2) \ 2, (thumbnailheight - h2) \ 2, w2, h2)
|
78
|
+
End Using
|
79
|
+
End Using
|
80
|
+
|
81
|
+
Return canvas
|
82
|
+
End Function
|
83
|
+
|
84
|
+
|
85
|
+
Private Sub CreateCache()
|
86
|
+
cts = New CancellationTokenSource
|
87
|
+
cachetask = Task.Factory.StartNew(Sub()
|
88
|
+
Try
|
89
|
+
|
90
|
+
For Each item In listitem
|
91
|
+
If thumbnailCache.ContainsKey(item.Text) = False Then
|
92
|
+
Dim thumbnail As Image = createThumbnail(item.Text)
|
93
|
+
SyncLock listlock
|
94
|
+
If thumbnailCache.ContainsKey(item.Text) = False Then
|
95
|
+
thumbnailCache.Add(item.Text, thumbnail)
|
96
|
+
End If
|
97
|
+
End SyncLock
|
98
|
+
Threading.Thread.Sleep(0)
|
99
|
+
cts.Token.ThrowIfCancellationRequested()
|
100
|
+
End If
|
101
|
+
Next
|
102
|
+
|
103
|
+
Catch ex As OperationCanceledException
|
104
|
+
Console.WriteLine("キャッシュ作成キャンセル")
|
105
|
+
End Try
|
106
|
+
|
107
|
+
End Sub, cts.Token)
|
108
|
+
|
109
|
+
End Sub
|
110
|
+
|
111
|
+
Private Sub ListView1_DrawItem(sender As Object, e As DrawListViewItemEventArgs) Handles ListView1.DrawItem
|
112
|
+
Dim filiename As String = e.Item.Text
|
113
|
+
Dim thumbnail As Image
|
114
|
+
|
115
|
+
'サムネイルキャッシュに含まれている場合はキャッシュを
|
116
|
+
'そうでない場合はサムネイルを作成、キャッシュに追加したのち描画する
|
117
|
+
If thumbnailCache.ContainsKey(filiename) Then
|
118
|
+
thumbnail = thumbnailCache(filiename)
|
119
|
+
Else
|
120
|
+
thumbnail = createThumbnail(filiename)
|
121
|
+
SyncLock listlock
|
122
|
+
If thumbnailCache.ContainsKey(filiename) = False Then
|
123
|
+
thumbnailCache.Add(filiename, thumbnail)
|
124
|
+
End If
|
125
|
+
End SyncLock
|
126
|
+
End If
|
127
|
+
|
128
|
+
'アイテムの描画
|
129
|
+
Dim imagerect As New Rectangle(New Point(e.Bounds.X + ((e.Bounds.Width - thumbnail.Width) / 2), e.Bounds.Y), New Size(thumbnail.Width, thumbnail.Height))
|
130
|
+
|
131
|
+
e.DrawDefault = False
|
132
|
+
e.DrawBackground()
|
133
|
+
e.Graphics.DrawImage(thumbnail, imagerect)
|
134
|
+
|
135
|
+
Dim stringFormat = New StringFormat()
|
136
|
+
stringFormat.Alignment = StringAlignment.Center
|
137
|
+
stringFormat.LineAlignment = StringAlignment.Center
|
138
|
+
e.Graphics.DrawString(e.Item.Text, ListView1.Font, Brushes.Black, New RectangleF(e.Bounds.X, e.Bounds.Y + imagerect.Height + 5, e.Bounds.Width, e.Bounds.Height - imagerect.Height - 5), stringFormat)
|
139
|
+
|
140
|
+
e.DrawFocusRectangle()
|
141
|
+
End Sub
|
142
|
+
|
143
|
+
End Class
|
144
|
+
```
|