Imports System.IO
Public Class Form1
Function createThumbnail(ByVal image As Image, ByVal w As Integer, ByVal h As Integer) As Image
Dim canvas As New Bitmap(w, h)
Dim g As Graphics = Graphics.FromImage(canvas)
g.FillRectangle(New SolidBrush(Color.White), 0, 0, w, h)
Dim fw As Double = CDbl(w) / CDbl(image.Width)
Dim fh As Double = CDbl(h) / CDbl(image.Height)
Dim scale As Double = Math.Min(fw, fh)
Dim w2 As Integer = CInt(image.Width * scale)
Dim h2 As Integer = CInt(image.Height * scale)
g.DrawImage(image, (w - w2) \ 2, (h - h2) \ 2, w2, h2)
g.Dispose()
Return canvas
End Function
'
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim imageDir As String = "C:\"
Dim jpgFiles As String() = _
System.IO.Directory.GetFiles(imageDir, "*.jpg")
Dim width As Integer = 100
Dim height As Integer = 80
ImageList1.ImageSize = New Size(width, height)
ListView1.LargeImageList = ImageList1
For i As Integer = 0 To jpgFiles.Length - 1
Dim original As Image = Bitmap.FromFile(jpgFiles(i))
Dim thumbnail As Image = createThumbnail(original, width, height)
ImageList1.Images.Add(thumbnail)
ListView1.Items.Add(jpgFiles(i), i)
original.Dispose()
thumbnail.Dispose()
Next
End Sub
End Class