VB.net如何将图像文件转换为pdf文件,然后在不使用SaveFileDialog的情况下保存它?

时间:2015-07-21 07:33:01

标签: vb.net

我已经设法将图像文件转换为pdf文件..但我想让它自动保存到特定位置,而不会问我在哪里保存它。请帮助我,任何帮助将不胜感激。初学者在这里。这是我目前正在尝试的代码。

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Text
Imports System.Windows.Forms
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing
Imports System.IO

Public Class Form1


    Private Sub captureScreen()

        'Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim graph As Graphics = Nothing

        Try

            ' gets the upper left hand coordinate of the form
            Dim frmleft As System.Drawing.Point = Me.Bounds.Location

            'use the commented out version for the full screen
            'Dim bmp As New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)

            'this version get the size of the form1  The + 8 adds a little to right and bottom of what is captured.
            Dim bmp As New Bitmap(Me.Bounds.Width + 8, Me.Bounds.Height + 8)

            'creates the grapgic
            graph = Graphics.FromImage(bmp)

            'Gets the x,y coordinates from the upper left start point
            'used below  
            Dim screenx As Integer = frmleft.X
            Dim screeny As Integer = frmleft.Y

            ' The - 5 here allows more of the form to be shown for the top and left sides.
            graph.CopyFromScreen(screenx - 5, screeny - 5, 0, 0, bmp.Size)

            ' Save the Screenshot to a file
            bmp.Save("I:\eQA\temp.png")

            bmp.Dispose()
            graph.Dispose()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        captureScreen()

        ' Create new pdf document and page
        Dim doc As New PdfDocument()
        Dim oPage As New PdfPage()


        ' Add the page to the pdf document and add the captured image to it
        doc.Pages.Add(oPage)
        'Dim xgr As XGraphics = XGraphics.FromPdfPage(oPage)
        Dim img As XImage = XImage.FromFile("I:\eQA\temp.png")
        'xgr.DrawImage(img, 0, 0)

        'Create XImage object from file.
        Using xImg = PdfSharp.Drawing.XImage.FromFile("I:\eQA\temp.png")
            'Resize page Width and Height to fit image size.
            oPage.Width = xImg.PixelWidth * 72 / xImg.HorizontalResolution
            oPage.Height = xImg.PixelHeight * 72 / xImg.HorizontalResolution

            'Draw current image file to page.
            Dim xgr = PdfSharp.Drawing.XGraphics.FromPdfPage(oPage)
            xgr.DrawImage(xImg, 0, 0, oPage.Width, oPage.Height)

        End Using

        ' instantiate a Bitmap object
        Dim filesaveas As New SaveFileDialog
        filesaveas.Filter = ("PDF File|*.pdf")


        Dim btnSave As DialogResult = filesaveas.ShowDialog()
        If btnSave.Equals(DialogResult.OK) Then
            doc.Save(filesaveas.FileName)
            doc.Close()
        End If

        ' I used the Dispose() function to be able to 
        ' save the same form again, in case some values have changed.
        ' When I didn't use the function, an GDI+ error occurred.
        img.Dispose()

    End Sub
End Class

0 个答案:

没有答案