0

I have created a Visual Basic .NET application that takes a screenshot every second and saves it to a file screenshot.jpg And I also created a server that serves the screenshot.jpg file every second. But the problem is that sometimes I see half of the picture (probably because it hasn't finished drawing yet). Is there a way around this problem? I wonder how webcams offer that smooth stream. Does it mean my screenshot capture program is too slow?

enter image description here

Here is my application

' Take screenshot every second
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim bmp As Bitmap = ScreenCap()

    bmp.Save(Application.StartupPath & "\screenshot.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
    bmp.Dispose()
End Sub

You can see the full project on Github: https://git.io/Jv4TL

And here is the front end code

<html>
<body>
    <img class="screenshot" src="screenshot.jpg">

    <script>
        setInterval(() => {
            document.querySelector('.screenshot').src =
                'screenshot.jpg?id=' + Math.random(0, 1000)
        }, 1000)
    </script>
</body>
</html>
Liga
  • 3,291
  • 5
  • 34
  • 59
  • If youbsuspect it's cause thebdilenisnt finished being written, perhaps save the screenshot with the number of seconds of the current time embedded and have the server request the n-1 file? – Caius Jard Feb 17 '20 at 18:42
  • Read the notes [here](https://stackoverflow.com/a/53026765/7444103). The DpiAwareness part and how the VirtualScreen size is calculated (the way you're currently measuring the Screen Size(s) can be quite wrong). Note that `Graphics.CopyFromScreen()` already perform a `BitBlt` copy of the current Desktop DC to the underlying DC (the Bitmap it generates from). A DpiAware app can also create Bitmaps with the same Dpi of the current Desktop DC. Of course, each Screen can have its own Dpi definition. So, you may want to limit the capture to the current Screen alone. – Jimi Feb 17 '20 at 20:38
  • A WinForms app can capture (with normal means - which includes calling `BitBlt` directly, not much of a difference, since, as already mentioned, that's what happens anyway) up to ~27-29 frames per second (considering the whole Screen). More than enough to generate a good animation. Transferring the captured sequences over the network is another matter. Btw, do not save the images as JPEG, use the native PNG format. – Jimi Feb 17 '20 at 20:48
  • @Jimi ````PNG```` format takes 3x times more space than a ````jpg```` – Liga Feb 18 '20 at 07:00

0 Answers0