Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Files getting replaced

 

I am using the following code to take a screenshot of the window when the user clicks a button but im having a problem.. when the form is closed and then reopened it will start over with the numbering and replace existing screenshots..

       Dim Type As String
       Try
           If System.IO.File.Exists(Application.StartupPath & "\Sidekick Files\Settings\Screenshot.dat") = True Then
               Dim srType As System.IO.StreamReader = _
           New System.IO.StreamReader(Application.StartupPath & "\Sidekick Files\Settings\Screenshot.dat")
               Dim line, vals() As String
               Do
                   line = srType.ReadLine
                   If line Is Nothing Then Exit Do
                   vals = line.Split(vbTab)
                   Type = vals(0)
               Loop
               srType.Close()
               srType = Nothing
           ElseIf System.IO.File.Exists(Application.StartupPath & "\Sidekick Files\Settings\Screenshot.dat") = False Then
               Type = "bmp"
           End If

           If Type = "bmp" Then
               'Does the events required to copy the screenshot to the file
               SendKeys.Send("{PRTSC}")
               Application.DoEvents() '/// give it a chance to be added to the clipboard.
               Dim bmp As Bitmap = Clipboard.GetDataObject.GetData(DataFormats.Bitmap)
               Dim path As String = Application.StartupPath & "\Sidekick Files\Image Viewer\ScreenShot" & intCount & ".bmp"
               bmp.Save(Application.StartupPath & "\Sidekick Files\Image Viewer\ScreenShot" & intCount & ".bmp", Imaging.ImageFormat.Bmp)
               intCount += 1
               'Displays Image has been captured.
               MessageBox.Show("Image has been captured! You can go to View then Image Viewer to see this image.")
           ElseIf Type = "jpg" Then
               'Does the events required to copy the screenshot to the file
               SendKeys.Send("{PRTSC}")
               Application.DoEvents() '/// give it a chance to be added to the clipboard.
               Dim bmp As Bitmap = Clipboard.GetDataObject.GetData(DataFormats.Bitmap)
               Dim path As String = Application.StartupPath & "\Sidekick Files\Image Viewer\ScreenShot" & intCount & ".jpg"
               bmp.Save(Application.StartupPath & "\Sidekick Files\Image Viewer\ScreenShot" & intCount & ".jpg", Imaging.ImageFormat.Jpeg)
               intCount += 1
               'Displays Image has been captured.
               MessageBox.Show("Image has been captured! You can go to View then Image Viewer to see this image.")
           End If
       Catch
           MsgBox("Could not take a screenshot. Please try again.")
       End Try

 

if someone could add in what i need to fix this and then post it it would help me alot.. thanks in advance :)

Edited by Lanc1988
Posted (edited)

i have all of the following code at the top just below the Windows Form Designer generated code

 

   Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
   Private Const VK_SNAPSHOT As Integer = &H2C
   Private intCount As Integer = 0

 

i would think adding in the system.io.file.exists somehow and if it was true it would just add another number to the file until it finds one that doesnt exist but i cant figure out how to add it in to my code :confused:

Edited by Lanc1988
  • Administrators
Posted

Something like

Dim intcount as integer
Dim path As String  & intCount & ".bmp"

Do
intCount+=1
path = Application.StartupPath & "Sidekick FilesImage ViewerScreenShot"
Until System.IO.File.Exists = false

 

not tested it (don't have VS on this pc) but the general idea should work

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted (edited)

ok.. ive been looking at my code and the code you posted but i cant figure out where to add it in to mine.. where would it go in this?:

               'Does the events required to copy the screenshot to the file
               SendKeys.Send("{PRTSC}")
               Application.DoEvents() '/// give it a chance to be added to the clipboard.
               Dim bmp As Bitmap = Clipboard.GetDataObject.GetData(DataFormats.Bitmap)
               Dim path As String = Application.StartupPath & "\Sidekick Files\Image Viewer\ScreenShot" & intCount & ".bmp"
               bmp.Save(Application.StartupPath & "\Sidekick Files\Image Viewer\ScreenShot" & intCount & ".bmp", Imaging.ImageFormat.Bmp)
               intCount += 1
               'Displays Image has been captured.
               MessageBox.Show("Image has been captured! You can go to View then Image Viewer to see this image.")

 

 

I tried the following code but it doesnt work:

               'Does the events required to copy the screenshot to the file
               SendKeys.Send("{PRTSC}")
               Application.DoEvents() 'give it a chance to be added to the clipboard.
               Dim bmp As Bitmap = Clipboard.GetDataObject.GetData(DataFormats.Bitmap)
               Dim path As String = Application.StartupPath & "\Sidekick Files\Image Viewer\ScreenShot" & intCount & ".bmp"
               Do
                   intCount += 1
               Loop Until System.IO.File.Exists(Application.StartupPath & "\Sidekick Files\Image Viewer\ScreenShot" & intCount & ".bmp" = False)
               bmp.Save(Application.StartupPath & "\Sidekick Files\Image Viewer\ScreenShot" & intCount & ".bmp", Imaging.ImageFormat.Bmp)
               MessageBox.Show("Image has been captured! You can go to View then Image Viewer to see this image.")

Edited by Lanc1988
  • Administrators
Posted

Any chance of telling us what error it gives? .Net can throw several different errors any of which could indicate a different problem...

 

I'm guessing you want the exit condition of the loop to be

Do
   intCount += 1
Loop Until System.IO.File.Exists(Application.StartupPath & "Sidekick FilesImage ViewerScreenShot" & intcount & ".bmp") = False

 

Also you might want to add

Option Strict On

to the top of your source files to catch more of these errors at compile time rather than runtime.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

ok, when i used this code it will take the screenshot with no errors but it is still replacing existing screenshots.. like i open the form take a few screenshots then when i close and reopen the form it starts over and replaces them.

 

here is the code i tried:

               'Does the events required to copy the screenshot to the file
               SendKeys.Send("{PRTSC}")
               Application.DoEvents() 'give it a chance to be added to the clipboard.
               Dim bmp As Bitmap = Clipboard.GetDataObject.GetData(DataFormats.Bitmap)
               Dim path As String = Application.StartupPath & "\Sidekick Files\Image Viewer\ScreenShot" & intCount & ".bmp"
               Do
                   intCount += 1
               Loop Until System.IO.File.Exists(Application.StartupPath & "Sidekick Files\Image Viewer\ScreenShot" & intCount & ".bmp") = False
               bmp.Save(Application.StartupPath & "\Sidekick Files\Image Viewer\ScreenShot" & intCount & ".bmp", Imaging.ImageFormat.Bmp)
               MessageBox.Show("Image has been captured! You can go to View then Image Viewer to see this image.")

  • Leaders
Posted

I copied and pasted the code in your previous post, and it worked like a charm.

 

Also, unless I am missing something, couldn't you store the number you left off at in the registry or in a config file?

 

And just a note: I noticed that you are taking screen shots by simulating a Print-Screen, which overwrites the clipboard. I recommend using BitBlt to copy from the desktop hDC to an hDC created by a graphics object.

[sIGPIC]e[/sIGPIC]
Posted

i could store it in a file but some users dont always have access to write to a file unless they are an admin on their computer.

 

hmm.. which code in my previous post? i have posted a few different ones above all of which arent working for me.. did you close the form them reopen it? because my problem isnt taking the screenshot, its stopping it from replacing existing ones.

  • Leaders
Posted
i could store it in a file but some users dont always have access to write to a file unless they are an admin on their computer.

[/Quote]

Any user should have permissions to create files in their own application data folders or registry hive if the program is being run locally. After all, if your program can save images, why can't it save a config file?

hmm.. which code in my previous post? i have posted a few different ones above all of which arent working for me.. did you close the form them reopen it? because my problem isnt taking the screenshot, its stopping it from replacing existing ones.

By "your previous post" (as opposed to "one of your previous posts"), I was referring to your most recent post...

 

Anyways, I copied it directly, compiled it without changing it, took five screen shots, lost the contents of my clipboard, deleted two random images, ran the program again, and it filled in the blanks that I made by deleting the files. My best guess is that the bug exists elsewhere in your code.

[sIGPIC]e[/sIGPIC]
Posted
hmm.. well ive posted all my code that is related to the screenshot code.. maybe ill copy it all into a new project and post the project here so you can see whats it doing.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...