簡體   English   中英

vb.net代碼捕獲Word文檔首頁的屏幕截圖

[英]vb.net code to capture screen shot of word document first page

您好,下面是可以捕獲PNG格式的Word文檔首頁圖像的步驟。 如果用戶需要以任何其他格式捕獲圖像。 然后,在下面的代碼中,將圖像擴展名替換為所需的輸出圖像擴展名。 如果這個答案有用,那么別忘了投票。

第一步:首先,您需要在項目的名稱空間下導入/引用:

    Import Imports Microsoft.Office.Interop.Word

第二步:在項目中添加空白類文件,然后將以下vb.net代碼復制到Word文檔中:

Public Class ClipboardAPI
<Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="OpenClipboard", SetLastError:=True, ExactSpelling:=True, CallingConvention:=System.Runtime.InteropServices.CallingConvention.StdCall)> _
Public Shared Function OpenClipboard(ByVal hWnd As IntPtr) As Boolean
End Function

<Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="EmptyClipboard", SetLastError:=True, ExactSpelling:=True, CallingConvention:=System.Runtime.InteropServices.CallingConvention.StdCall)> _
Public Shared Function EmptyClipboard() As Boolean
End Function

<Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="SetClipboardData", SetLastError:=True, ExactSpelling:=True, CallingConvention:=System.Runtime.InteropServices.CallingConvention.StdCall)> _
Public Shared Function SetClipboardData(ByVal uFormat As Integer, ByVal ByValhWnd As IntPtr) As IntPtr
End Function

<Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="CloseClipboard", SetLastError:=True, ExactSpelling:=True, CallingConvention:=System.Runtime.InteropServices.CallingConvention.StdCall)> _
Public Shared Function CloseClipboard() As Boolean
End Function

<Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="GetClipboardData", SetLastError:=True, ExactSpelling:=True, CallingConvention:=System.Runtime.InteropServices.CallingConvention.StdCall)> _
Public Shared Function GetClipboardData(ByVal uFormat As Integer) As IntPtr
End Function

<Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="IsClipboardFormatAvailable", SetLastError:=True, ExactSpelling:=True, CallingConvention:=System.Runtime.InteropServices.CallingConvention.StdCall)> _
Public Shared Function IsClipboardFormatAvailable(ByVal uFormat As Integer) As Short
End Function
End Class

第三步:這是僅從第一頁從Word文檔捕獲圖像的代碼。

    Dim Width, Height, Orientation as Integer
    Dim objWord As New Microsoft.Office.Interop.Word.Application
    Dim objDoc As Microsoft.Office.Interop.Word.Document

    Const CF_ENHMETAFILE As Integer = 14
    objDoc = objWord.Documents.Open(YourSourcePath)

    objWord.ActiveDocument.Select()
    objWord.Selection.CopyAsPicture()

    Try
        Dim ip As IntPtr
        Dim metaFile As System.Drawing.Imaging.Metafile
        Dim bRet As Boolean
        bRet = ClipboardAPI.OpenClipboard(Me.Handle)
        If bRet = True Then
            'Verify the clipboard contains data available
            'as an enhanced metafile.
            bRet = ClipboardAPI.IsClipboardFormatAvailable(CF_ENHMETAFILE) <> 0
        End If

        If bRet = True Then
            'Store the clipboard's contents in the IntPtr.
            ip = ClipboardAPI.GetClipboardData(CF_ENHMETAFILE)
        End If

        'Verify the IntPrt contains data before proceeding. Passing
        'an empty IntPtr to System.Drawing.Imaging.Metafile results
        'in an exception.
        If Not IntPtr.Zero.Equals(ip) Then
            metaFile = New System.Drawing.Imaging.Metafile(ip, True)
            ClipboardAPI.CloseClipboard()
            Dim image As System.Drawing.Image = metaFile
            'Me.PictureBox1.Image = metaFile

            Dim objImageWriter As Image = New Bitmap(image)
            Dim objGraphics As Graphics = Graphics.FromImage(objImageWriter)
            objGraphics.Clear(Color.White)
            objGraphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias

            If Orientation = 1 Then
                Width = image.Width
                Height = image.Height
            Else
                Width = image.Height
                Height = image.Width
            End If

            objGraphics.DrawImage(image, 0, 0, Width, Height)

            image.Dispose()
            objGraphics.Dispose()

            Dim ep As Imaging.EncoderParameters = New Imaging.EncoderParameters
            ep.Param(0) = New System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100)

            Dim codecs() As Imaging.ImageCodecInfo = Imaging.ImageCodecInfo.GetImageEncoders()
            Dim iciInfo As Imaging.ImageCodecInfo
            Dim item As Imaging.ImageCodecInfo

            For Each item In codecs
                If (item.MimeType = "image/png") Then iciInfo = item
            Next

            ImageFileName = Format(Now, "ddMMMyyyy_hhmmss_") & RandNumber.Next & ".png"
            ImageFilePath = Application.StartupPath & "\" & ImageFileName
            objImageWriter.Save(ImageFilePath, iciInfo, ep)
            objImageWriter.Dispose()

        End If
    Catch ex As Exception
        ExceptionGenerated = True
        Throw New System.Exception(ex.Message.ToString())
    Finally
        objDoc.Close()
        objWord.Application.Quit(False)
        objDoc = Nothing
        objWord = Nothing
        ReleaseComObject(objWord)
        ReleaseComObject(objDoc)
    End Try

第四步:在項目中添加ReleaseComObject函數:

    Public Sub ReleaseComObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        End Try
    End Sub

您甚至可以使用以下vb.net代碼檢查單詞頁面的方向:

    objDoc.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait / wdOrientLandscape

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM