Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27192

generate thumbnail from documents and other files

$
0
0
So i have done alot of research on google and found no answer for this question.

How do you create a thumbnail image of all file types to use in a image box.

there were plenty of posts found on google with people attempting this back in like 2006 -07 -09 but no one back then got a solution so its now 2013 wonder if any 1 has got a solution.


basically i have a function that gets thumbnails for images and vectors and such but nothing for documents or movie files or pdf etc......


this is what i have most likely not that great but it works basically it attempts to create the thumbnail and if it can not then it will get the files icon.

not the best solution but its a work around untill i can find something to generate the thumbnails for all file types.

Code:


Imports System.IO
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices
Imports System.Drawing.Icon

Module Iconextract

    Private Structure SHFILEINFO
        Public hIcon As IntPtr            ' : icon
        Public iIcon As Integer          ' : icondex
        Public dwAttributes As Integer    ' : SFGAO_ flags
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
        Public szDisplayName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
        Public szTypeName As String
    End Structure

    Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" _
            (ByVal pszPath As String, _
            ByVal dwFileAttributes As Integer, _
            ByRef psfi As SHFILEINFO, _
            ByVal cbFileInfo As Integer, _
            ByVal uFlags As Integer) As IntPtr

    Private Const SHGFI_ICON = &H100
    Private Const SHGFI_SMALLICON = &H1
    Private Const SHGFI_LARGEICON = &H0    ' Large icon
    Private nIndex = 0



    Public Function GetThumbnail(ByRef itemname As String, ByVal path As String) As Image

        Dim imgThumb As Image = Nothing
        Try
            Dim image As Image = Nothing

            image = image.FromFile(path)

            ' Check if image exists
            If Not image Is Nothing Then

                imgThumb = image.GetThumbnailImage(125, 80, Nothing, New IntPtr())
                drawimagebox(itemname, imgThumb)

            End If

        Catch

            Dim hImgLarge As IntPtr  'The handle to the system image list.
            Dim fName As String      'The file name to get the icon from.
            Dim shinfo As SHFILEINFO
            shinfo = New SHFILEINFO()
            fName = path

            shinfo.szDisplayName = New String(Chr(0), 260)
            shinfo.szTypeName = New String(Chr(0), 80)

            hImgLarge = SHGetFileInfo(fName, 0, shinfo, _
                        Marshal.SizeOf(shinfo), _
                        SHGFI_ICON Or SHGFI_LARGEICON)


            Dim myIcon As System.Drawing.Icon
            myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)

            Dim bmp As Bitmap = myIcon.ToBitmap()

        End Try

    End Function


End Class


Viewing all articles
Browse latest Browse all 27192

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>