0

I am struggling to find a way on how to get any file thumbnail into my userforms picturebox (The image visible in windows explorer) using visual basic.

I have only found how to do that for image files

 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())

But it doesn't work for any other type of files.

Could someone, please,help me with this code?

Eduards
  • 68
  • 2
  • 20
  • 1
    Check out [this](https://stackoverflow.com/a/1751610/1797425) it will save you time and exactly what you need. – Trevor Dec 21 '21 at 15:15
  • 1
    [SHGetFileInfo](https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shgetfileinfow) -- [SHGetImageList](https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shgetimagelist) – Jimi Dec 21 '21 at 15:15
  • I specified that I'm working with visual basic not C# so I'm not sure it will work but I'm trying it out – Eduards Dec 21 '21 at 15:16
  • @Eduards it does work, I just did a test with it. – Trevor Dec 21 '21 at 15:17
  • It throws me 10 errors so could you please post an answer with modified code that worked for you, mate? – Eduards Dec 21 '21 at 15:18
  • What framework are you targeting? Also post code you are using. – Trevor Dec 21 '21 at 15:23
  • Newest? 5.0 or something – Eduards Dec 21 '21 at 15:24
  • 1
    `Dim sfile As ShellFile = ShellFile.FromFilePath(FILENAMEHERE) Dim thumb As Bitmap = sfile.Thumbnail.ExtraLargeBitmap` and make sure `Imports Microsoft.WindowsAPICodePack.Shell` is at the top of your class. Other than that there's no other code I had to change. Did you get that NuGet package from that answer I linked? – Trevor Dec 21 '21 at 15:25
  • Ok now there's just 2 errors. "Type shellfile is not defined" and "shellfile is not declared" as well afetr importing the windowsapicodepackshell doesnt contain any public members – Eduards Dec 21 '21 at 15:29
  • 1
    I've added code below that's been adapted from the link provided by @Zaggler – Tu deschizi eu inchid Dec 21 '21 at 16:02

1 Answers1

2

Try the following which is adapted from C# get thumbnail from file via windows api

Download/install NuGet package 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

Resources

Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24
  • How do you install that Microsoft-WindowsAPICodePack-Shell? – Eduards Dec 22 '21 at 06:36
  • 1
    @Eduards It's available as a NuGet package. [Installing NuGet Packages In Visual Studio](https://www.c-sharpcorner.com/article/install-package-in-visual-studio-for-solution/). – Andrew Morton Dec 22 '21 at 12:05