C# SCROLLINFO not working on Chrome window

Ankur Tripathi 1 Reputation point
2023-08-13T10:53:38.2166667+00:00

I am attempting to retrieve scroll information from the window handle, but I keep getting a value of 0. I am pursuing this approach because my goal is to capture a complete scrolling screenshot of the window. The intention is to enable automatic scrolling and image capture. While I have successfully implemented automatic window scrolling, I've encountered variations in the scrolling behaviour for different windows.

For instance, sending a mouse delta of 100 results in Chrome scrolling by around 70 pixels, whereas Visual Studio scrolls by approximately 110 pixels. Each window appears to exhibit distinct scrolling characteristics. Therefore, my current objective is to calculate the precise amount of pixel movement during scrolling using the scrollbar. However, I have encountered difficulties in achieving this functionality.

Basically, I want to scroll screenshots.. so I have 2 options 1) After scrolling automatically by code calculate how much pixel scroll moved and I will capture that part and attach it to the image 2) If I am able to scroll pre-define height so, in that case, I know I scrolled 100 pixels so capture a 100-pixel image only trying option 1 but no luck yet..

Getting "False" from GetScrollInfo and GetLastError() returns 1447 error code

Code:-

// get scrollbar info
SCROLLINFO scrollinfo = new SCROLLINFO();
scrollinfo.cbSize = (uint)Marshal.SizeOf(scrollinfo);
scrollinfo.fMask = SIF_ALL; // Get all the scroll bar information.
bool horBar = GetScrollInfo1(windowHandle, SB_HORZ, out scrollinfo);
Console.WriteLine(horBar);
bool vertBar = GetScrollInfo1(windowHandle, SB_VERT, out scrollinfo);
Console.WriteLine(vertBar);
Console.WriteLine(scrollinfo.nPage + ", " + scrollinfo.nPos);

[DllImport("user32.dll")]
public static extern bool GetScrollInfo(IntPtr hWnd, int fnBar, ref SCROLLINFO lpsi);
Windows development | Windows API - Win32
Developer technologies | .NET | Other
Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,766 Reputation points Volunteer Moderator
    2023-08-13T15:31:58.7933333+00:00

    The scale of the scrollbar is typically based on the size of the offscreen content. As your code has no way to detect the offscreen size, you can not calculate the number of pixels the scroll moves. Take chrome for instance, the length of the html document will control movement of the scrollbar.


  2. Castorix31 90,686 Reputation points
    2023-08-15T09:36:03.9833333+00:00

    my goal is to capture a complete scrolling screenshot of the window.

    You can use the same method as in ShareX : if GetScrollInfo fails, it compares the last 2 images to detect end of scrolling

    https://github.com/ShareX/ShareX/blob/51c471113d6f5a64acc00b542ad78a0f9783ce99/ShareX.ScreenCaptureLib/Helpers/ScrollingCaptureManager.cs

    (I tested with Chrome on Windows 10 22H2)

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.