Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I switched from Windows 7 to Windows 10 for VB.NET code maintenance just yesterday and found that SaveFromScreen was behaving strangely. Now I have found that the window which was 600 pixels in height is being shown to be 601 pixels and that messes up the logic of resizing and saving a picture from the screen.

Is there a good way to handle this? I have these things on some 15-20 forms.

What I have tried:

If Me.Height = 600 or Me.Height = 601 Then

- this would solve the problem here, but this is probably not how things should be, and I would have to do that on 15-20 forms. And what do I do about the location, which is completely wrong?

Edit: Added from solution 2:

Here is the code. I am of course writing bad code since I am a beginner and far from a full time programmer, but am glad to learn how to write better code. I would also be happy to learn how to make this a control which I can put on a lot of forms.
VB.NET
  1  Dim filN As String = "p83.png"
  2  
  3      Call Label20_DoubleClick(sender, e) : Me.Refresh()
  4      Call SaveFromScreen(Me, filN)
  5      Call Label20_DoubleClick(sender, e)
  6  
  7  Private Sub Label20_DoubleClick(sender As Object, e As EventArgs) Handles Label20.DoubleClick
  8  
  9      Dim YN As Boolean = False
 10  
 11      If Me.Height = 600 Then YN = True
 12      Me.WindowState = FormWindowState.Normal
 13  
 14      If YN Then
 15          Me.Height = plotarea.Bottom + 110
 16          Me.Width = plotarea.Right + plotarea.Left + 16
 17      Else
 18          Me.Height = 600
 19          Me.Width = 800
 20      End If
 21  
 22      If plotarea.Height > 250 Then
 23          If Me.Height = plotarea.Bottom + 110 Then YN = True ' posed for photograph
 24          If YN Then ' restore
 25              Me.Height = plotarea.Bottom + 260
 26              Me.Width = Screen.PrimaryScreen.Bounds.Width
 27          Else ' pose for photograph
 28              Me.Height = plotarea.Bottom + 110
 29              Me.Width = plotarea.Right + plotarea.Left + 16
 30          End If
 31      End If
 32  
 33  End Sub
 34  
 35  Sub SaveFromScreen(frm As Form, filn As String)
 36  
 37      Dim img As New Bitmap(frm.ClientSize.Width, frm.ClientSize.Height)
 38  
 39      Using gr As Graphics = Graphics.FromImage(img)
 40          gr.CopyFromScreen(frm.PointToScreen(frm.ClientRectangle.Location), Point.Empty, frm.ClientSize)
 41      End Using
 42  
 43      img.Save(filn, Imaging.ImageFormat.Png)
 44  
 45  End Sub

This was working fine on Windows 7, but does not work on Windows 10. Thanks for your help.
Posted
Updated 17-Oct-22 0:34am
v2

If your code cannot handle a different window size of a single pixel, you've written so bad code.

It's impossible for anyone to tell you how to fix this because, well, we can't see your code. If you have this in 15 to 20 forms, it sounds like this is something that should have been made into a control and not duplicated code 15 to 20 times.
 
Share this answer
 
Comments
VBeginner.NET 16-Oct-22 2:08am    
I am putting the code as a response since this textbox does not seem to take anything other than plain text.
Here is the code. I am of course writing bad code since I am a beginner and far from a full time programmer, but am glad to learn how to write better code. I would also be happy to learn how to make this a control which I can put on a lot of forms.

VB
    Dim filN As String = "p83.png"

    Call Label20_DoubleClick(sender, e) : Me.Refresh()
    Call SaveFromScreen(Me, filN)
    Call Label20_DoubleClick(sender, e)

Private Sub Label20_DoubleClick(sender As Object, e As EventArgs) Handles Label20.DoubleClick

    Dim YN As Boolean = False

    If Me.Height = 600 Then YN = True
    Me.WindowState = FormWindowState.Normal

    If YN Then
        Me.Height = plotarea.Bottom + 110
        Me.Width = plotarea.Right + plotarea.Left + 16
    Else
        Me.Height = 600
        Me.Width = 800
    End If

    If plotarea.Height > 250 Then
        If Me.Height = plotarea.Bottom + 110 Then YN = True ' posed for photograph
        If YN Then ' restore
            Me.Height = plotarea.Bottom + 260
            Me.Width = Screen.PrimaryScreen.Bounds.Width
        Else ' pose for photograph
            Me.Height = plotarea.Bottom + 110
            Me.Width = plotarea.Right + plotarea.Left + 16
        End If
    End If

End Sub

Sub SaveFromScreen(frm As Form, filn As String)

    Dim img As New Bitmap(frm.ClientSize.Width, frm.ClientSize.Height)

    Using gr As Graphics = Graphics.FromImage(img)
        gr.CopyFromScreen(frm.PointToScreen(frm.ClientRectangle.Location), Point.Empty, frm.ClientSize)
    End Using

    img.Save(filn, Imaging.ImageFormat.Png)

End Sub


This was working fine on Windows 7, but does not work on Windows 10. Thanks for your help.
 
Share this answer
 
Comments
Dave Kreskowiak 16-Oct-22 11:22am    
Fist, the correct place to put this code would have been your question. Just hove the mouse over your question, then click the green "Improve question" link.

Next, you're code is written very specifically to hard code values. If a user, like you've found, has a different version of Windows, or changes their Windows theme, or changes the display properties to compensate for high resolution displays, your code breaks.

This code would have to be rewritten to take into account window client dimensions, but worse, it looks like you should be creating controls to do some of this stuff instead of drawing directly on a form. Controls containerize parts of your view, or what you see on screen, and enable you greater control over the data they present and how they present it, like fitting in variable dimensions, like window size and scaling.

Also, your SaveFromScreen method should not be saving, well, from the screen. The image you're using should be held separately as a source (original) Bitmap object, and anything you draw on screen or save as a file should be drawn/saved from that Bitmap object. If you have to change the dimensions of the image before saving, it's trivial to draw the source Bitmap onto another Bitmap object and save the image from that instead.

Hard coded, or "magic" numbers, are bad and should be avoided. You coming back to the code, or someone taking over the project from you, would have no idea what those values are. They should be replaced with constants so the name of the constant describes what the value is for.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900