Selenium C# PhantomJs can't change viewport height - c#

i have Problem setting up Viewport on PhantomJS using Selenium.
webDriver.Navigate().GoToUrl("http://detectmybrowser.com/");
webDriver.Manage().Window.Size = new Size(1920, 1080);
webDriver.TakeScreenshot().SaveAsFile("screenshot.png", ImageFormat.Png);
But when i check my browser resolution on detectmybrowser i get a resolution of 1920x4128.
Look
I tryed to maximize the window but this didn't change anything. Please help :)

PhantomJS automatically expands the viewport in the scroll direction. It should report the configured viewport height when requesting document.viewport.getHeight() (as used on the site you're on), but it reports the expanded height. Another way to get the proper viewport height would be through document.documentElement.clientHeight and window.innerHeight.

Related

Trouble understanding render targets and back buffers in monogame

My graphics device is set up as follows:
GraphicsDeviceManager = new GraphicsDeviceManager(GameBase.GameRef);
GraphicsDeviceManager.PreferredBackBufferWidth = 640;
GraphicsDeviceManager.PreferredBackBufferHeight = 480;
GraphicsDeviceManager.ApplyChanges();
I can't find anywhere on google explaining exactly what the preferred back buffer width means.
I set up a render target like this:
RenderTarget = new RenderTarget2D(
GraphicsDeviceManager.GraphicsDevice,
640,
480,
false,
GraphicsDeviceManager.GraphicsDevice.PresentationParameters.BackBufferFormat,
DepthFormat.Depth24);
I can't find anything on google about what the 640 and 480 actually mean with respect to how the render target is drawn.
The problem I have is that if I resize the window, everything stretches. I'm trying to understand what monogame is doing to cause this, and then finally figure out a solution.
At the start my window is 640x480 and the PreferredBackBufferWidth and height are 640/480 and the render target is 640/480
If I resize the window to 1920x1080 for example, everything stretches, so somehow monogame no longer rendering the 640x480 texture onto a 640x480 part of the screen, but I haven't changed any of the values or done any code for this.
If I resize the screen, should the render target be destroyed and recreated at the new size of the window, or should the back buffer width and height be changed, or both, or something else?
I am very confused and there isn't any documentation online technical enough to answer in detail. Once I understand it I should be able to write some code so that when you resize the screen nothing happens, everything stay the same size, and there is a black area around the right/bottom edge because the window is larger than what i'm rendering to.
The back buffer is a special render target that is displayed on the screen. It is stretched to fill the window, which results in scaling if the window size does not match.
You can resize the back buffer when the window size changes. The Game.Window.ClientSizeChanged event is fired when that happens (note that it may be fired multiple times if the user is resizing the window by dragging). Then resizing the back buffer is simply
GraphicsDeviceManager.PreferredBackBufferWidth = Window.ClientBounds.Width;
GraphicsDeviceManager.PreferredBackBufferHeight = Window.ClientBounds.Height;
GraphicsDeviceManager.ApplyChanges();
Whether you should recreate the RenderTarget depends on what you're using it for.

Graphics CopyFromScreen off when font/icon scaling is enabled

Need some help with C# Windows Forms program. This is on .NET 4.0.
I use the following (snippet of) code to capture a composited image from my window:
var location = PointToScreen(picVisualizer.Location);
...
g.CopyFromScreen(location, new Point(0, 0), picVisualizer.Size, CopyPixelOperation.SourceCopy);
picVisualizer is a PictureBox control in my window.
This works perfectly as intended, as long as the user keeps Windows font/icon scaling at 100%. If they change, the image captured is the right size but is offset, capturing part of the window, the desktop, etc.
I've tried a variety of solutions but they always give me the same location value, so the end result is the same. I've also tried AutoScaleMode to None, Font, Dpi and Inherit, and all four gave me the same result.
Is there a way to tweak that code so it'll capture the correct part of the screen even when display scaling is not at 100%?
Thanks.
The answer suggested by Blorgbeard works beautifully.
picVisualizer.DrawToBitmap(bitmap, picVisualizer.ClientRectangle);
Thanks.

Move System.Drawing.Rectangle to a different area on Screen

I am creating a screen capture & crop utility using answer posted here
The code draws a rectangle (System.Drawing.Rectangle) on the screen and saves a cropped image.
I need to make this rectangle movable to a different area on the screen without changing the size.
How can I achieve this?
What did not work for me?
I tried this codeproject article, works very good to move controls on the screen.
For this code to work correctly I would need to draw rectangle on a container control.
Which container can be used to wrapping?
Being a succesful web developer I used to think that I can write for WinForms also... I was wrong :-( Some help would be much appreciated!
In C# Windows Forms to change the location of a rectangle you do the following
rect.Location = new Location(x,y);
Where x and y are the coordinates (relative to the window's origin) that you want to move it to.
Here's a link to The MSDN Page on System.Drawing.Rectangle

How can I use Window.Eval to get height and width of window into c#?

I need to get the window width and height in Internet Explorer
Initially I was using a nice simple statement
var height = HtmlPage.Window.Eval("innerHeight").ToString();
However I then found out that this doesnt work in IE 8
There appear to be loads of different ways to get the width and height of a window, I am not sure which is the best, but the one below is mentioned here
Get the window height
doc = document;
var theHeight = Math.max(
doc.body.scrollHeight, doc.documentElement.scrollHeight,
doc.body.offsetHeight, doc.documentElement.offsetHeight,
doc.body.clientHeight, doc.documentElement.clientHeight
);
My question is how can I put this into Window.Eval to get the height?
What does the syntax need to be?
If there is a better, more reliable way of getting window width and height please let me know, but it needs to work for different browsers
Paul
untested. just adapting your code...
HtmlPage.Window.Eval("Math.max( document.body.scrollHeight, document.documentElement.scrollHeight, document.body.offsetHeight, document.documentElement.offsetHeight, document.body.clientHeight, document.documentElement.clientHeight )").ToString();

Taking screenshots in Windows Vista, Windows 7, with transparent areas outside the app region

I am trying to take a screenshot of an application and I would like to make the parts of the rectangle that are not part of the applications region be transparent. So for instance on a standard windows application I would like to make the rounded corners transparent.
I wrote a quick test application which works on on XP (or vista/windows 7 with aero turned off):
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
// Just find a window to test with
IntPtr hwnd = FindWindowByCaption(IntPtr.Zero, "Calculator");
WINDOWINFO info = new WINDOWINFO();
info.cbSize = (uint)Marshal.SizeOf(info);
GetWindowInfo(hwnd, ref info);
Rectangle r = Rectangle.FromLTRB(info.rcWindow.Left, info.rcWindow.Top, info.rcWindow.Right, info.rcWindow.Bottom);
IntPtr hrgn = CreateRectRgn(info.rcWindow.Left, info.rcWindow.Top, info.rcWindow.Right, info.rcWindow.Bottom);
GetWindowRgn(hwnd, hrgn);
// fill a rectangle which would be where I would probably
// write some mask color
g.FillRectangle(Brushes.Red, r);
// fill the region over the top, all I am trying to do here
// is show the contrast between the applications region and
// the rectangle that the region would be placed in
Region region = Region.FromHrgn(hrgn);
region.Translate(info.rcWindow.Left, info.rcWindow.Top);
g.FillRegion(Brushes.Blue, region);
}
When I run this test app on XP (or Vista/Windows 7 with Aero off), I get something like this, which is great because I can eek an xor mask out of this that can be used later with BitBlt.
removed dead Imageshack link - Screenshot
Here is the problem, on Vista or Windows 7 with Aero enabled, there isn't necessarily a region on the window, in fact in most cases there isn't. Can anybody help me figure out some way to build a mask like this on these platforms?
Here are some of the approaches I have already tried...
1. Using the PrintWindow function: This doesn't work because it gives back a screenshot taken of the window with Aero off and this window is a different shape from the window returned with Aero on
2 Using the Desktop Window Manager API to get a full size thumbnail: This didn't work because it draws directly to the screen and from what I can tell you can't get a screenshot directly out of this api. Yeah, I could open a window with a pink background, show the thumbnail, take a screenshot then hide this temporary window but thats a horrible user experience and a complete hack I would rather not have my name on.
3. Using Graphics.CopyFromScreen or some other pinvoke variant of this: This doesn't work because I can't assume that the window I need information from is at the top of the z-order on the screen.
Right now, the best solution I can think of is to special case Aero on Windows 7 and Vista to manually rub out the corners by hard coding some graphics paths I paint out but this solution would suck since any application that performs custom skinning will break this.
Can you think of another or better solution?
If you are here, thanks for taking time to read this post, I appreciate any help or direction that you can offer!
If you are looking for a finished application, there is 7capture, which captures also the translucency, so images can be saved to PNG format for later compositing.
EDIT:
The original question and comments indicate you are looking to produce a region on Windows Vista/7 that you can then use to mask out parts the captured image, as is done with Windows XP and non-Aero UIs. Using a region is not going to give you the result you are looking for, since the window outline is not computed as a region, but as an image with variable transparency - RGBA. The Alpha channel in that image is your mask, but it's not an on-off mask like a region, but a gradual mask with a range of values from pixels being fully included to being fully masked out.
Although it uses undocumented APIs, the code at http://spazzarama.wordpress.com/2009/02/12/screen-capture-with-vista-dwm/ will capture to a RGBA buffer which you can then use to render or save the image with the shadow and other translucency effects intact.
In DwmCapture.cs Change
BackBufferFormat = Format.X8R8G8B8
to
BackBufferFormat = Format.A8R8G8B8
(X8->A8)
And you should then be able to access both the usual RGB data plus transparency from the captured buffer. This can then be saved as a PNG or other format with alpha-channel for composing.
Removed idea that is terrible but would have been awesome back in the '90s
You say that using the DWM API only allows you to capture directly to the screen... could you create a window offscreen (say, X = -100000px, Y = -100000px) but visible (maybe even hidden?) and draw the screenshot to it? Since when using the DWM each window has a backing texture, I'm thinking it might still get drawn fine even though the target isn't directly onscreen.
Also, if you want to go the DirectX route and access the actual DX texture backing the window, I found a few leads that might help (especially the first link):
http://spazzarama.wordpress.com/2009/02/12/screen-capture-with-vista-dwm/
http://channel9.msdn.com/forums/TechOff/251261-Help-Getting-the-shared-window-texture-out-of-DWM-/
http://web.archive.org/web/20080625183653/http://www.aeroxp.org/board/index.php?showtopic=6286
http://www.youtube.com/watch?v=hRTgFTMnT_U
Using Graphics.CopyFromScreen or some other pinvoke variant of this:
This doesn't work because I can't
assume that the window I need
information from is at the top of the
z-order on the screen.
If you know which window you need the information from, can you bring it to the front, call Graphics.CopyFromScreen, and then reset its z-index? I know from experience that Aero does odd things when items are in the background in order to make their glass interface work correctly (partial rendering etc). This may not be great UX; however, it would be a special case and used only when Aero is turned on.
You can take a look at the source code of AeroShot, as described on the main page, it can capture rounded edges and with the Aero Glass transparency effect and save it to a PNG file. It's written in C#.

Categories