VB.NET 以编程方式获取图片框中的任何文件缩略图作为图像图像

VB.NET get any file thumbnail in picturebox as an image image programmatically

我正在努力寻找一种方法来使用 visual basic 将任何文件缩略图放入我的用户窗体图片框(windows 资源管理器中可见的图像)。

我只找到了如何对图像文件执行此操作

 Dim image As Image = New Bitmap(file) 'File is a full path to the file

 'Resize and preserve aspect ratio
  Dim Ratio As Double = CDbl(image.Width / image.Height)
  Dim H As Integer = 150
  Dim W As Integer = CInt(H / Ratio)

  'Set image
  .Image = image.GetThumbnailImage(H, W, callback, New IntPtr())

但它不适用于任何其他类型的文件。

有人可以帮我写这段代码吗?

尝试以下改编自C# get thumbnail from file via windows api

Download/install NuGet 包 Microsoft-WindowsAPICodePack-Shell

Imports Microsoft.WindowsAPICodePack.Shell
Imports System.IO
                            ...

Private Function GetThumbnailBytes(filename As String, desiredHeight As Integer) As Byte()
    Dim thumbnailBytes As Byte()

    Using sfile As ShellFile = ShellFile.FromFilePath(filename)
        Dim thumbBmp As Bitmap = sfile.Thumbnail.ExtraLargeBitmap

        'compute new width
        Dim Ratio As Double = CDbl(thumbBmp.Width / thumbBmp.Height)
        Dim height As Integer = desiredHeight
        Dim width As Integer = CInt(height / Ratio)

        'resize
        Using resizedBmp As Bitmap = New Bitmap(thumbBmp, width, height)
            Using ms As MemoryStream = New MemoryStream()
                resizedBmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
                thumbnailBytes = ms.ToArray()
            End Using
        End Using
    End Using

    Return thumbnailBytes
End Function

Private Sub btnRun_Click(sender As Object, e As EventArgs) Handles btnRun.Click

    Using ofd As OpenFileDialog = New OpenFileDialog()
        If ofd.ShowDialog() = DialogResult.OK Then

            Dim thumbnailBytes = GetThumbnailBytes(ofd.FileName, 60)
            Dim thumbnail = GetThumbnail(ofd.FileName, 60)

            Using ms As MemoryStream = New MemoryStream(thumbnailBytes)
                PictureBox1.Image = Image.FromStream(ms)
                PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
            End Using
        End If
    End Using
End Sub

资源

  • C# get thumbnail from file via windows api