dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: Trying to take a screen capture of a game running in BlueStacks using C#

  1. #1
    Join Date
    Nov 2019
    Posts
    1

    Trying to take a screen capture of a game running in BlueStacks using C#

    I have inherited a game trainer written in C#, the game was originally a mobile game (Android and IOS) and was ported to the Windows store. The trainer works fine with the game that is downloaded form the Windows store but the support for the Windows platform has not been updated and I am trying to update the code to work with the Android emulator "Bluestacks" with the game running within it.

    The original app used FindWindow & CopyFromScreen to take a snapshot of the game window and then as the situation in the game dictates it checks pixels to interact with the game based on the current screen state. The problem I am running into is getting a valid capture of the screen. using Spy++ I can see that there are child windows under the main Bluestacks window and when the game is opened I and I tell Spy++ to highlight the window there are two different child windows that flash. I have tried using both and I still get the wrong pixel values for coordinates I am checking in the game window so I'm either not at the correct window or I'm not taking the capture correctly.

    Here is what Spy++ returns:
    Name:  SpyPlus.jpg
Views: 114
Size:  11.9 KB

    This is what is working to capture the screen in the native Windows version of the game and allows the pixel state to be correctly checked.:

    Code:
     
    private void GetGameWindow()
    		{
    			Form1.Rect rectangle = default(Form1.Rect);
    			IntPtr intPtr = FindWindow("ApplicationFrameWindow", "Game Window");
    			SetForegroundWindow(intPtr);
    			GetWindowRect(intPtr, ref rectangle);
    			top = rectangle.Top;
    			left = rectangle.Left;
    			width = rectangle.Right - rectangle.Left;
    			height = rectangle.Bottom - rectangle.Top;
    			UIClass.ClickBar(this);
    		}
    
    		public void BringToFront()
    		{
    			IntPtr foregroundWindow = FindWindow("ApplicationFrameWindow", "Game Window");
    			SetForegroundWindow(foregroundWindow);
    		}
    
    		public Bitmap Capture()
    		{
    			Bitmap bitmap = new Bitmap(Width, Height);
    			Rectangle rectangle = new Rectangle(Left, Top, width, height);
    			using (Graphics graphics = Graphics.FromImage(bitmap))
    			{
    				graphics.CopyFromScreen(new Point(rectangle.Left, rectangle.Top), Point.Empty, rectangle.Size);
    			}
                return bitmap;
            }
    This is what I have tried to used to acquire the game window running in Bluestacks in order to check the various pixels inside the game window. I have tried to capture both the second and fourth level windows using FindWindowEx since they were the tow that highlighted the actual game window in the emulator via Spy++.

    Code:
    private void GetGameWindow()
    		{
    			Form1.Rect rectangle = default(Form1.Rect);
    			IntPtr intPtr = FindWindow("HwndWrapper[Bluestacks.exe;;c71f55d0-1ed0-4f5a-bde9-5427f675dccc]", "BlueStacks");
    			SecondBSWin = FindWindowEx(intPtr, IntPtr.Zero, "WindowsForms10.Window.8.app.0.34f5582_r6_ad1", "BlueStacks Android PluginAndroid");
                ThirdBSWin = FindWindowEx(SecondBSWin, IntPtr.Zero, "WindowsForms10.EDIT.app.0.34f5582_r6_ad1", null);
                ForthBSWin = FindWindowEx(ThirdBSWin, IntPtr.Zero, "BlueStacksApp", "_ctl.Window");
                SetForegroundWindow(SecondBSWin);
                GetWindowRect(SecondBSWin, ref rectangle);
    			top = rectangle.Top;
    			left = rectangle.Left;
    			width = rectangle.Right - rectangle.Left;
    			height = rectangle.Bottom - rectangle.Top;
    			UIClass.ClickBar(this);
    		}
                                
            public void BringToFront()
    		{
    			IntPtr foregroundWindow = FindWindow("HwndWrapper[Bluestacks.exe;;c71f55d0-1ed0-4f5a-bde9-5427f675dccc]", "BlueStacks");
    			SetForegroundWindow(foregroundWindow);
                SecondBSWin = FindWindowEx(foregroundWindow, IntPtr.Zero, "WindowsForms10.Window.8.app.0.34f5582_r6_ad1", "BlueStacks Android PluginAndroid");
                ThirdBSWin = FindWindowEx(SecondBSWin, IntPtr.Zero, "WindowsForms10.EDIT.app.0.34f5582_r6_ad1", null);
                ForthBSWin = FindWindowEx(ThirdBSWin, IntPtr.Zero, "BlueStacksApp", "_ctl.Window");
    
            }
                      
            public Bitmap Capture()
    		{
    			Bitmap bitmap = new Bitmap(Width, Height);
    			Rectangle rectangle = new Rectangle(Left, Top, width, height);
    			using (Graphics graphics = Graphics.FromImage(bitmap))
    			{
    				graphics.CopyFromScreen(new Point(rectangle.Left, rectangle.Top), Point.Empty, rectangle.Size);
    			}
    			return bitmap;
            }
    Any thoughts or suggestions would be appreciated.
    Last edited by 2kaud; November 18th, 2019 at 11:48 AM. Reason: Added code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)