n.Mehta n.Mehta - 4 months ago 9
Vb.net Question

creating more than one image file when button clicked

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


I want to save an image when the user clicks the print button, but it creates two images at once since the images is already there. I only want to create another image if the print button is clicked again. How to I make it so that the code the increments the file name only runs if the print button is clicked again?

Answer

Comment the following line;

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

because you are saving the image at the end of the code.

Comments