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

VB.net如何将图像文件转换为pdf文件,然后在不使用SaveFileDialog的情况下保存?,vb.net,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

我已经设法将图像文件转换为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

如果您不想使用
保存文件对话框
,则不要显示它。只需删除这些行,然后使用包含文件保存位置的
字符串
变量调用
doc.Save
。我不确定你的问题是什么。我的问题是我不想使用SaveFileDialog,我只想让它直接将PDF保存到某个目录,而不显示任何showdialog,并让我保存文件。您自己创建SaveFileDialog时使用以下行:
Dim filesaveas As New savefile dialog
,然后使用以下行显示它:
Dim btnsavas DialogResult=filesaveas.showdialog()
。如果你不想这样,那就删除那些行。好的,谢谢。。我知道,但没关系,我终于知道怎么做了。谢谢你的建议^^如果您不想使用
保存文件对话框
,则不要显示它。只需删除这些行,然后使用包含文件保存位置的
字符串
变量调用
doc.Save
。我不确定你的问题是什么。我的问题是我不想使用SaveFileDialog,我只想让它直接将PDF保存到某个目录,而不显示任何showdialog,并让我保存文件。您自己创建SaveFileDialog时使用以下行:
Dim filesaveas As New savefile dialog
,然后使用以下行显示它:
Dim btnsavas DialogResult=filesaveas.showdialog()
。如果你不想这样,那就删除那些行。好的,谢谢。。我知道,但没关系,我终于知道怎么做了。谢谢你的建议^^