What's new

C/C++ In need of some help... (Capturing a partial screenshot of the screen)

  • Thread starter xXBeefyDjXx
  • Start date
  • Views 617
xXBeefyDjXx

xXBeefyDjXx

Long time Sinner
Administrator
Hidden Devils
MotM 10th Anniversary Mr. Nice Guy
Messages
1,963
Solutions
6
Reaction score
721
Points
1,135
Sin$
-7
So Let me put this into context first, I am designing a bot for Piano Tiles 2 using C#, and figured that it'd be easiest to make it click the tiles using colour detection in a screenshot.

I have managed to make the bot work and press the blue start tile. (Hooray!)

But. It is capturing the whole screen, when in practice it only needs to capture a small line across the app to detect the scrolling tiles.

I would leave it capturing the whole screen but this is inefficient, and also because it scrolls from the top, down, this makes the bot hit the wrong tile, as it needs to scan from the bottom up, and hit the first tile nearest the bottom.

I also know once I have the optimised code for capturing the screen I would need to make it loop the code, but that is simple in practice and will be implemented later on.


My main question is how on earth do I make it capture a customisable section instead of the whole screen?

Here is the current code:

//Captures the screen, Crucial to bot running. DO NOT BREAK.
//Warning: This code is unoptimised and produces shots of the whole screen.
private Bitmap Screenshot()
{
Bitmap PTView = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);

Graphics g = Graphics.FromImage(PTView);

g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);

return PTView;

}
 
Twain

Twain

Kamehameha!
Messages
707
Reaction score
257
Points
190
Sin$
0
So Let me put this into context first, I am designing a bot for Piano Tiles 2 using C#, and figured that it'd be easiest to make it click the tiles using colour detection in a screenshot.

I have managed to make the bot work and press the blue start tile. (Hooray!)

But. It is capturing the whole screen, when in practice it only needs to capture a small line across the app to detect the scrolling tiles.

I would leave it capturing the whole screen but this is inefficient, and also because it scrolls from the top, down, this makes the bot hit the wrong tile, as it needs to scan from the bottom up, and hit the first tile nearest the bottom.

I also know once I have the optimised code for capturing the screen I would need to make it loop the code, but that is simple in practice and will be implemented later on.


My main question is how on earth do I make it capture a customisable section instead of the whole screen?

Here is the current code:

//Captures the screen, Crucial to bot running. DO NOT BREAK.
//Warning: This code is unoptimised and produces shots of the whole screen.
private Bitmap Screenshot()
{
Bitmap PTView = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);

Graphics g = Graphics.FromImage(PTView);

g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);

return PTView;

}
I remember Piano Tiles as a mobile game, so i guess u use a emulator to run it, that emulator window is filled up with the game screen so capturing the content of the window should be what you are looking for, im currently on the phone so i cant test anything, but after a quick google search i came up with This and i think it could be what you are looking for, sry for not being able to help u out with my own/tested code...
 
xXBeefyDjXx

xXBeefyDjXx

Long time Sinner
Administrator
Hidden Devils
MotM 10th Anniversary Mr. Nice Guy
Messages
1,963
Solutions
6
Reaction score
721
Points
1,135
Sin$
-7
I remember Piano Tiles as a mobile game, so i guess u use a emulator to run it, that emulator window is filled up with the game screen so capturing the content of the window should be what you are looking for, im currently on the phone so i cant test anything, but after a quick google search i came up with This and i think it could be what you are looking for, sry for not being able to help u out with my own/tested code...
I have tried the method suggested and it does not seem to work well with the code I already have...

Any other tips? I managed to implement the suggested code but when referenced to run it would throw an error saying it could not find the reference point.

This line here is the reference point
public static Bitmap Screenshot(IntPtr hwnd)

I would call it using " Bitmap PTView = Screenshot(); " and it would throw an error. Any ideas?

I haven't even tried to edit the code to see if it works for the window I want it to because I can't get it to run.
 
Twain

Twain

Kamehameha!
Messages
707
Reaction score
257
Points
190
Sin$
0
I have tried the method suggested and it does not seem to work well with the code I already have...

Any other tips? I managed to implement the suggested code but when referenced to run it would throw an error saying it could not find the reference point.

This line here is the reference point
public static Bitmap Screenshot(IntPtr hwnd)

I would call it using " Bitmap PTView = Screenshot(); " and it would throw an error. Any ideas?

I haven't even tried to edit the code to see if it works for the window I want it to because I can't get it to run.
hmm i cant really tell, maybe try addibg a new before the Screenshot() since you are initializing a new Indtance of Bitmap, thats all i can think off atm, more complicated but what would work 2, you select the area where the game is being displayed save the bounds of it the take a whole screen screenshot and crop it to the before located window size, that would be harder and if u move the game window it wouldnt work anymore, but thats the only solution i can comeup with atm...
 
xXBeefyDjXx

xXBeefyDjXx

Long time Sinner
Administrator
Hidden Devils
MotM 10th Anniversary Mr. Nice Guy
Messages
1,963
Solutions
6
Reaction score
721
Points
1,135
Sin$
-7
hmm i cant really tell, maybe try addibg a new before the Screenshot() since you are initializing a new Indtance of Bitmap, thats all i can think off atm, more complicated but what would work 2, you select the area where the game is being displayed save the bounds of it the take a whole screen screenshot and crop it to the before located window size, that would be harder and if u move the game window it wouldnt work anymore, but thats the only solution i can comeup with atm...
It's beginning to drive me up the wall that something that sounds so simple to actually do, is damn hard to code into another program, lol
 
Top Bottom
Login
Register