1

I am trying to do a screen capture using the code below

    Dim area As Rectangle = FormPrintArea.Bounds
    Dim areaWidth As Integer = CInt(area.Width * ScaleFactor)
    Dim areaHeight As Integer = CInt(area.Height * ScaleFactor)
    Dim capture As Bitmap = New Bitmap(areaWidth, areaHeight, PixelFormat.Format32bppArgb)
    Dim graph As Graphics = Graphics.FromImage(capture)
    Dim ScaledSize As New Size With {
        .Height = areaHeight,
        .Width = areaWidth
    }
    graph.CopyFromScreen(area.X, area.Y, 0, 0, ScaledSize, CopyPixelOperation.SourceCopy)
    PictureBox1.Image = capture

`` The problem is I can't find ScaleFactor, Everything works perfect as long as "Change the size of text, apps and the other items: 100%" in Windows 10 Display Settings but if it is set differently like 125% (recommended), I lose about 20% of the image. It looks like there is a ScaleFactor in the LanguageFont class but I can't seem to access it from VB (or C#).

The application has a VB Form (FormPrintArea) that the user uses to define the print area. If I set ScaleFactor to 1.25 on systems set to 125% (recommended), then everything works. Is there any way to get the value from Windows through an API? ScaleFactor 1

ScaleFactor 1.25

Paul Cohen
  • 241
  • 4
  • 14
  • What is the `ScaleFactor` and why do you need it? Can you post images for comparison? – γηράσκω δ' αεί πολλά διδασκόμε Dec 16 '16 at 04:28
  • In Windows 10, Customize your display, the first setting allows users to make stuff (Text, apps and other items) larger or smaller. On a HD display (1920x1024) the default is 125%. In the code above if this is set to 100% the applications works correctly and the size of the form FormPrintArea = what is captured by the Graphics.FromImage(capture). If this value is set to something else (125% as in the default) less of the screen is captured, if I set ScaleFactor to 1.25 then everything gets captured correctly. What I want is to get this value (ScaleFactor) from Windows through an API. – Paul Cohen Dec 17 '16 at 05:36
  • In the two photos above the form FormPrintArea was not changed in size or moved. visually on screen it looks like the second image and when the display setting is set to 125% with ScaleFactor=1 you get the first image. With the display setting set to 100% and ScaleFactor=1 you get the second (correct image). – Paul Cohen Dec 17 '16 at 05:50
  • 1
    You have to declare your app [to be dpiAware](http://stackoverflow.com/a/13228495/17034) so the OS does not have to lie about the window sizes anymore. – Hans Passant Dec 17 '16 at 10:28

1 Answers1

1

I needed to declare my App DPIaware as @Hans Passant said, the easiest way is to add the lines below to the Partial Class startup form (Form1).

<DllImport("User32.dll")> Private Shared Sub SetProcessDPIAware() End Sub

Then change the New Sub at the end of the startup form to call SetProcessDPIAware

Public Sub New() SetProcessDPIAware() InitializeComponent() End Sub

Once you do that you can get the ScaleFactor in Form1.Load as follows DPIScalingX = Me.CreateGraphics().DpiX / 96 DPIScalingY = Me.CreateGraphics().DpiY / 96

Paul Cohen
  • 241
  • 4
  • 14