View word document as thumbnails in VB.Net / ASP.Net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sejal17
    New Member
    • Dec 2007
    • 56

    View word document as thumbnails in VB.Net / ASP.Net

    Hello Friends,

    I want Application that displays an image preview of a Word document (Like thumbnail View of word document)

    Following is my code but i got error on bold part of code


    [code=vbnet]
    Imports System
    Imports System.Text
    Imports System.Drawing. Imaging.BitmapD ata
    Imports System.Runtime. InteropServices
    Imports System.IO
    Imports System.Web.UI.W ebControls.Imag e

    Partial Class vb
    Inherits System.Web.UI.P age


    Public Shared Function GetThumbnailIma ge(ByVal fileName As String, ByVal longestEdge As Integer, ByVal colorDepth As Integer) As Image

    Dim desktopFolder As IShellFolder
    Dim someFolder As IShellFolder
    Dim extract As IExtractImage
    Dim pidl As IntPtr
    Dim filePidl As IntPtr
    Dim MAX_PATH As IntPtr


    'Manually define the IIDs for IShellFolder and IExtractImage
    Dim IID_IShellFolde r = New Guid("000214E6-0000-0000-C000-000000000046")
    Dim IID_IExtractIma ge = New Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1")

    'Divide the file name into a path and file name
    Dim folderName = Path.GetDirecto ryName(fileName )
    Dim shortFileName = Path.GetFileNam e(fileName)

    'Get the desktop IShellFolder
    ShellInterop.SH GetDesktopFolde r(desktopFolder )

    'Get the parent folder IShellFolder
    desktopFolder.P arseDisplayName (IntPtr.Zero, IntPtr.Zero, folderName, 0, pidl, 0)
    desktopFolder.B indToObject(pid l, IntPtr.Zero, IID_IShellFolde r, someFolder)

    'Get the file's IExtractImage
    someFolder.Pars eDisplayName(In tPtr.Zero, IntPtr.Zero, shortFileName, 0, filePidl, 0)
    someFolder.GetU IObjectOf(IntPt r.Zero, 1, filePidl, IID_IExtractIma ge, 0, extract)

    'Set the size
    Dim size As SIZE
    size.cx = 500
    size.cy = 500

    Dim flags = IEIFLAG.ORIGSIZ E Or IEIFLAG.QUALITY
    Dim bmp As IntPtr
    Dim thePath = Marshal.AllocHG lobal(MAX_PATH)


    'Interop will throw an exception if one of these calls fail.
    Try
    extract.GetLoca tion(thePath, MAX_PATH, 0, size, colorDepth, flags)
    extract.Extract (bmp)
    Catch ex As Exception
    End Try


    'Free the global memory we allocated for the path string
    Marshal.FreeHGl obal(thePath)

    'Free the pidls. The Runtime Callable Wrappers
    'should automatically release the COM objects
    Marshal.FreeCoT askMem(pidl)
    Marshal.FreeCoT askMem(filePidl )

    If Not bmp.Equals(IntP tr.Zero) Then
    GetThumbnailIma ge = Image.FromHbitm ap(bmp)- Error (FromHbitmap is not a member of System.Web.UI.W ebControls.Imag e)
    ' GetThumbnailIma ge = Image(bmp)

    Else
    GetThumbnailIma ge = Nothing
    End If
    End Function

    Public Enum IEIFLAG As Integer
    ASYNC = &H1
    CACHE = &H2
    ASPECT = &H4
    OFFLINE = &H8
    GLEAM = &H10
    SCREEN = &H20
    ORIGSIZE = &H40
    NOSTAMP = &H80
    NOBORDER = &H100
    QUALITY = &H200
    End Enum

    <StructLayout(L ayoutKind.Seque ntial)> _
    Public Structure STRRET_CSTR
    Public uType As Integer
    <FieldOffset(4) , MarshalAs(Unman agedType.LPWStr )> _
    Public pOleStr As String
    <FieldOffset(4) > _
    Public uOffset As Integer
    <FieldOffset(4) , MarshalAs(Unman agedType.ByValA rray, SizeConst:=520) > _
    Public strName As Byte()
    End Structure

    <StructLayout(L ayoutKind.Seque ntial)> _
    Public Structure SIZE
    Public cx As Integer
    Public cy As Integer
    End Structure

    <ComImportAttri bute(), _
    GuidAttribute(" BB2E617C-0920-11d1-9A0B-00C04FC2D6C1"), _
    InterfaceTypeAt tribute(ComInte rfaceType.Inter faceIsIUnknown) > _
    Public Interface IExtractImage

    Sub GetLocation( _
    ByVal pszPathBuffer As IntPtr, _
    ByVal cch As Integer, _
    ByRef pdwPriority As Integer, _
    ByRef prgSize As SIZE, _
    ByVal dwRecClrDepth As Integer, _
    ByRef pdwFlags As Integer)

    Sub Extract(ByRef phBmpThumbnail As IntPtr)

    End Interface

    <ComImportAttri bute(), _
    GuidAttribute(" 000214E6-0000-0000-C000-000000000046"), _
    InterfaceTypeAt tribute(ComInte rfaceType.Inter faceIsIUnknown) > _
    Public Interface IShellFolder

    Sub ParseDisplayNam e( _
    ByVal hWnd As IntPtr, _
    ByVal pbc As IntPtr, _
    ByVal pszDisplayName As String, _
    ByRef pchEaten As Integer, _
    ByRef ppidl As System.IntPtr, _
    ByRef pdwAttributes As Integer)

    Sub EnumObjects( _
    ByVal hwndOwner As IntPtr, _
    <MarshalAs(Unma nagedType.U4)> ByVal grfFlags As Integer, _
    <Out()> ByRef ppenumIDList As IntPtr)

    Sub BindToObject( _
    ByVal pidl As IntPtr, _
    ByVal pbcReserved As IntPtr, _
    ByRef riid As Guid, _
    ByRef ppvOut As IShellFolder)

    Sub BindToStorage( _
    ByVal pidl As IntPtr, _
    ByVal pbcReserved As IntPtr, _
    ByRef riid As Guid, _
    <Out()> ByVal ppvObj As IntPtr)

    <PreserveSig( )> _
    Function CompareIDs( _
    ByVal lParam As IntPtr, _
    ByVal pidl1 As IntPtr, _
    ByVal pidl2 As IntPtr) As Integer

    Sub CreateViewObjec t( _
    ByVal hwndOwner As IntPtr, _
    ByRef riid As Guid, _
    ByVal ppvOut As Object)

    Sub GetAttributesOf ( _
    ByVal cidl As Integer, _
    ByVal apidl As IntPtr, _
    <MarshalAs(Unma nagedType.U4)> ByRef rgfInOut As Integer)

    Sub GetUIObjectOf( _
    ByVal hwndOwner As IntPtr, _
    ByVal cidl As Integer, _
    ByRef apidl As IntPtr, _
    ByRef riid As Guid, _
    <Out()> ByVal prgfInOut As Integer, _
    <Out(), MarshalAs(Unman agedType.IUnkno wn)> ByRef ppvOut As Object)

    Sub GetDisplayNameO f( _
    ByVal pidl As IntPtr, _
    <MarshalAs(Unma nagedType.U4)> ByVal uFlags As Integer, _
    ByRef lpName As STRRET_CSTR)

    Sub SetNameOf( _
    ByVal hwndOwner As IntPtr, _
    ByVal pidl As IntPtr, _
    <MarshalAs(Unma nagedType.LPWSt r)> ByVal lpszName As String, _
    <MarshalAs(Unma nagedType.U4)> ByVal uFlags As Integer, _
    ByRef ppidlOut As IntPtr)

    End Interface

    Public Class ShellInterop
    <DllImport("she ll32.dll", CharSet:=CharSe t.Auto)> _
    Public Shared Function SHGetDesktopFol der( _
    <Out()> ByRef ppshf As IShellFolder) As Integer
    End Function
    End Class


    End Class

    [/code]
    If anyone has code that display word document in Thumbnail view then please send me code...

    Thanx in Advance........ ..
  • CyberSoftHari
    Recognized Expert Contributor
    • Sep 2007
    • 488

    #2
    I hink this will help you:
    Microsoft Office Thumbnails in SharePoint
    (Do not post toomany code part.)

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Hi Sejal17,

      As a full member now, you should know that we expect your code to be posted in [CODE] tags (See How to Ask a Question).
      This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.
      Please use [CODE] tags in future.

      Also, as CyberSoftHari has pointed out, it is a lot easier to understand your problem if you post only the code that is relevant to the problem. Posting all of your code makes it harder to see the problem and harder to help you out.

      - Moderator Frinny

      Comment

      • nshergold
        New Member
        • Jun 2009
        • 1

        #4
        An answer hopefully

        Hi sejal,

        I was looking into doing this for my web application and couldn't find anything better than your post. I know its been a while since you posted. Well done peicing that together (I assume from the MSDN documentation). Your problem is that you want to return a System.Drawing. Image not a System.Web.UI.W ebControls.Imag e other than that this code should work.

        Thanks,
        Nat.

        Comment

        • mossmann
          New Member
          • Mar 2007
          • 3

          #5
          This code does not work. I have been trying it all day.
          I think that the MAX_PATH constant (see code line 19) is supposed to be an int, and not an intptr (although I'm not sure, the original code never initializes it...)
          I'm currently looking at another solution:



          http://www.vbaccelerat or.com/home/net/code/libraries/Shell_Projects/Thumbnail_Extra ction/article.asp




          It's also based around IExtractImage, and it sort of works... However, it does not work on pdfs...

          There should be a way to generate pdf thumbnails via imagemagick (google imagemagick pdf thumbnail )
          hovever that seems to be quite cumbersome, because , if I have understood this correctly, you would need to install imagemagick, and ghostscript in order for that to work.
          Plz if anyone has a fullproof solution to this, post it.
          (I am looking for something that can create thumbnails for .doc and .pdf)

          Comment

          • benaref
            New Member
            • Feb 2012
            • 1

            #6
            Did you find a solution for extracting thumb of docs and pdfs yet ?

            Comment

            Working...