单击按钮时创建多个图像文件

时间:2016-08-03 20:16:34

标签: vb.net

Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
        CaptureScreen()
        'used to created file path
        Dim orderNum As String = Val(txtNum.Text) 'gets value of Invoice/PO number
        Dim orderType As String = GroupBox3.Text 'gets value of either its a Invoice or PO`

        'saves printscreen to PictureBox
        PictureBox1.Image = bmpBackground
        'saves printscreen to file path 
        PictureBox1.Image.Save("\\PCLIQFS\Shared_Data\PCLiq Scale-Shots\" + orderType + " " + orderNum + ".jpg", Imaging.ImageFormat.Jpeg)

        'creates variable of filePath
        Dim filePath As String = "\\PCLIQFS\Shared_Data\PCLiq Scale-Shots\" + orderType + " " + orderNum + ".jpg"
        'checks to see if file is already in filePath
        If File.Exists(filePath) Then
            Dim folderPath = Path.GetDirectoryName(filePath)
            Dim fileName = Path.GetFileNameWithoutExtension(filePath)
            Dim extension = Path.GetExtension(filePath)
            Dim fileNumber = 0

            Do
                'increments file name 
                fileNumber += 1
                filePath = Path.Combine(folderPath,
                                        String.Format("{0} ({1}){2}",
                                                      fileName,
                                                      fileNumber,
                                                      extension))
            Loop While File.Exists(filePath)
        End If
        'saves new image
        PictureBox1.Image.Save(filePath)
    End Sub

我想在用户点击打印按钮时保存图像,但由于图像已经存在,因此它会一次创建两个图像。如果再次单击打印按钮,我只想创建另一个图像。如何设置,以便只在再次单击打印按钮时才会运行增加文件名的代码?

1 个答案:

答案 0 :(得分:1)

评论以下一行;

PictureBox1.Image.Save("\\PCLIQFS\Shared_Data\PCLiq Scale-Shots\" + orderType + " " + orderNum + ".jpg", Imaging.ImageFormat.Jpeg)

因为您要将代码保存在代码的末尾。