none
CopyFromScreen stretches image RRS feed

  • Question

  • Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

    Dim bitmap As Bitmap       

    Dim g As Graphics       

    bitmap = New Bitmap(400, 400)       

    g = Graphics.FromImage(bitmap)       

    Dim s As New Size(400, 400)       

    g.CopyFromScreen(200, 200, 0, 0, s)       

    PictureBox1.Image = bitmap   

    End Sub

    Copying a part of screen to PictureBox at the form.

    For some unknown reason CopyFromScreen stretches an image.

    This is a screenshot:

    How do I get a correct size of image?

    Thanks


    • Edited by Ha3R Friday, May 25, 2018 12:29 PM
    Friday, May 25, 2018 12:28 PM

All replies

  • Hello,

     The container object PictureBox property SizeMode will affect how images appear.

    Change accordingly to get the desire output. You may or may not need to also

    change the size of the PictureBox overall size.

     

     Hope this helps :)

    Friday, May 25, 2018 6:14 PM
  • I am not seeing the same issue. Perhaps you app is not DPI aware and your screen settings are at greater than 100% or something.

    Also you can post Visual Basic questions in the Visual Basic language forum.

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
            PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Using Bmp As New Bitmap(400, 400)
                Using g As Graphics = Graphics.FromImage(Bmp)
                    g.CopyFromScreen(200, 200, 0, 0, Bmp.Size)
                    PictureBox1.Image = CType(Bmp.Clone, Image)
                End Using
            End Using
        End Sub
    End Class


    La vida loca

    Saturday, May 26, 2018 5:52 PM
  • Yes, seems it's a DPI issue, I have font/pictures size set to 125%.

    But I'm not sure it's a Visual Basic problem:

    the same strange behavior with Win32 API functions: GetCursorPos function returns cursor position which differs position  which got from LParam when WM_MOUSEMOVE came in WndProc....

    If I set font/pictures size to 100% mouse coordinates are the same with both GetCursorPos function and WM_MOUSEMOVE message...

    LOL, it's incredible, it's a WINDOWS bullshit OS, mega LOL ))))

     

    Sunday, May 27, 2018 9:18 PM
  • Yes, seems it's a DPI issue, I have font/pictures size set to 125%.

    But I'm not sure it's a Visual Basic problem:

    the same strange behavior with Win32 API functions: GetCursorPos function returns cursor position which differs position  which got from LParam when WM_MOUSEMOVE came in WndProc....

    If I set font/pictures size to 100% mouse coordinates are the same with both GetCursorPos function and WM_MOUSEMOVE message...

    Hi,

    So have you solved your problem now?

    If so, hope you can close this thread by marking the reply as answer as this will help others looking for the same or similar issues down the road.

    Best Regards,

    Frankie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 28, 2018 2:07 AM
    Moderator