Reputation: 998
I have a WPF application in which I am attempting to take a screen grab of a WindowsFormsHost control. To do this I am doing a Graphics.CopyFromScreen. On most boxes we have tested on this works perfect, however we have one machine that is not grabbing the right size values. The width and height that are given by WPF do not match the actual width and height of the control. When snooping, the incorrect values show for the ActualWidth and ActualHeight of the control as well. Even further, when I snoop the main window (which is maximized), I get an ActualWidth of 1550 and and ActualHeight of 840, but my screen resolution is 1920x1080. I would understand being a few pixels off for borders, margins, etc, but to have WPF tell me my maximized window is 370 pixels shorter in width than the actual screen just doesn't make sense. Can anyone explain this behaviour?
The following is the code being used to capture an image of the control:
public static Bitmap CreateBitmapFromVisual(this FrameworkElement target)
{
Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
if (bounds.Width == 0 && bounds.Height == 0)
return null;
System.Windows.Point p0 = target.PointToScreen(bounds.TopLeft);
System.Drawing.Point p1 = new System.Drawing.Point((int) p0.X, (int) p0.Y);
Bitmap image = new Bitmap((int)bounds.Width, (int)bounds.Height);
Graphics imgGraphics = Graphics.FromImage(image);
imgGraphics.CopyFromScreen(p1.X, p1.Y, 0, 0, new System.Drawing.Size((int) bounds.Width, (int) bounds.Height));
return image;
}
Here is an image to better describe what I'm talking about:
As you can see, snoop is saying that the actualwidth and actualheight of the WindowsFormsHost is 486x336. Any debug information I log says the same thing. However, when I Print Screen and crop to the control in paint, the actual size is 608x423, a sizeable difference.
As a further update, it seems like the width given by wpf is roughly 80% of the actual width. This is true for both the control and the window.
Upvotes: 3
Views: 3410
Reputation: 2534
Have you looked at your DPI settings for your display?
To do this go to Control Panel - Display and select Set custom text size (DPI). In the dialog that opens you will see a scale to percentage. If it is not set at 100% then try this. This could be your issue.
Upvotes: 4