11 Replies - 4439 Views - Last Post: 22 March 2011 - 09:53 AM Rate Topic: -----

#1 rms.x95   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 13-March 11

[C#] Screenshot

Posted 14 March 2011 - 07:11 AM

Hello,

I started C# a few months ago and my first problem is coming.
I've a little problem with my project in Visual C# 2010 express.
I want to make a Screen capture program, so if I press "print screen", the program directly saves the image on a location, with a variable file name. The program needs to work on the background. But that's not the problem.

The problem is the screencapture.

- I don't need to get a part of the screen, only the full screen.
- I want a direct save sytem, as less as possible lag.

I've tried many topics from other forum's, but I don't have a solution yet.

Is there anyone who can tell me how to start?

Thanks in Advance.

Is This A Good Question/Topic? 0
  • +

Replies To: [C#] Screenshot

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: [C#] Screenshot

Posted 14 March 2011 - 07:35 AM

I'm sure you did the responsible thing by doing a google search before posting this; because you wouldn't really be just asking "Would someone do my research for me?"

You should have seen this posted on MSDN.
Did you try it?

Or the link it points to?
http://www.codeproje...TeboScreen.aspx
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: [C#] Screenshot

Posted 14 March 2011 - 07:36 AM

I've actually written a program like this. The information is out there; I didn't know how to do it either, but I searched and found the pieces. The problem is that you can't look for all of it as one search. You're not going to necessarily find one tutorial that teaches you everything you need to know. You might need to find one on running in the background, one on hooking keys, one on screen shotting, and one on writing to disk.

I wrote a tutorial on hotkeys; you'll need that to capture the PrintScreen button:

http://www.dreaminco...global-hotkeys/

I can also give you a bit more advice. Use the Graphics class. Specifically, the CopyFromScreen method.
Was This Post Helpful? 0
  • +
  • -

#4 rms.x95   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 13-March 11

Re: [C#] Screenshot

Posted 14 March 2011 - 08:04 AM

Thanks for fast reply!

@ tlhIn`toq
That's not true, I 've searched houres and houres, but I didn't find a solution, so I joined this forum.
I found many solutions, but none of them was working.
for example I found this: http://www.codeproje...urescreen1.aspx but the this isn't from the print screen key, and doesn't have an autosave, I made a stupid autosave on it but the variable filename didn't work. :(

@insertAlias
Thanks, I didn't found that yet!

For now I've enough information. I just found something I may can use on google too.
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: [C#] Screenshot

Posted 14 March 2011 - 08:08 AM

Quote

I found many solutions, but none of them was working.


Again, that's because you were looking for an all-in-one tutorial. You're going to have to branch out and figure parts of it out on your own. If someone already had a tutorial covering exactly what you wanted, what would be the point of following it? You could just download the executable and skip the whole "learning how to write it" part.

You're going to have to pull information from several different sources and put them all together. Good luck, and let us know if you have any more problems.
Was This Post Helpful? 1
  • +
  • -

#6 rms.x95   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 13-March 11

Re: [C#] Screenshot

Posted 14 March 2011 - 08:54 AM

Good help,
I know what you mean.
I started with: http://homeandlearn....arp/csharp.html (for complete beginners)
after I had done that, I have read some other tutorials.

I have made a text editor (With fontstyle/size/zoom/pictures/Spell check/ Margins/Page layout/save/open/tables/and a kind of Word Art, but that was failing :P.) I also had made a tab system (like a browser).

So I am not a complete beginner anymore.

But it's really not wrong to do some more tutorials!

Thanks,

PS: this forum has remarable good help!
Was This Post Helpful? 0
  • +
  • -

#7 rms.x95   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 13-March 11

Re: [C#] Screenshot

Posted 21 March 2011 - 01:06 PM

hmm, I made an autosave system, and only have a very little question about the "int":
If set an int with some zeros before the value:

int myint = 002;


And I put this for example to a textbox, but than appears 2, not 002!
how can i fix it?
Or do I need to do something like this:

                int filenm = 0001;
                if (filenm >= 9)
                {
                    numberz = "00" + filenm;
                }
                if (filenm >= 99)
                {
                    if (filenm <= 10)
                    {
                        numberz = "0" + filenm;
                    }
                }



I think there must exist an easier way! But where?
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: [C#] Screenshot

Posted 21 March 2011 - 01:17 PM

Much easier, using Format Strings.

string formatString = "000";
Console.WriteLine(1.ToString(formatString));
Console.WriteLine(11.ToString(formatString));
Console.WriteLine(111.ToString(formatString));
Console.WriteLine(1111.ToString(formatString));


Output:

Quote

001
011
111
1111


Remember that numeric data is stored as a number. Leading zeros (and trailing decimal zeros) are simply ignored as irrelevant, since they don't change a number. When you want to deal with an ouputted representation of that number, then you're dealing with strings. And there are premade methods for manipulating strings. In this case, we're using a format string.

The three zeros represent three significant non-decimal (it's an int, so there wouldn't be decimals anyway) figures. It will always show those three figures, even if the number is smaller than 100. If the number is larger, it doesn't matter.

This post has been edited by insertAlias: 21 March 2011 - 01:19 PM

Was This Post Helpful? 0
  • +
  • -

#9 rms.x95   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 13-March 11

Re: [C#] Screenshot

Posted 21 March 2011 - 01:37 PM

hmmm, I'll have a try...
thanks

thanks, it worked too, but I used:
newname = newname + 1;

            if (newname <= 9)
            {
                numbertje = "000" + newname;
            }
            if (newname <= 99)
            {
                if (newname >= 10)
                {
                    numbertje = "00" + newname;
                }
            }
            if (newname <= 999)
            {
                if (newname >= 100)
                {
                    numbertje = "0" + newname;
                }
            }
            textBox2.Text = numbertje;

and it works fine!

I have made a program to make 2D animations, which you can record from the screen, and saves it as 0001.jpg, 0002.jpg (It's a system for the free 3d gamemaker 3drad.com).
The program makes every xx seconds a screenshot, and saves it.

Before I can finish my project, I have a last and final question:
If I have a folder with for example 50 Jpeg's. My program needs to find that 0050.jpg is the last file. I need as output the value 0050. But I 'll need a very long code if I'm going to check if every file excists with an if statement. :(

thanks in advance
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: [C#] Screenshot

Posted 22 March 2011 - 09:25 AM

Quote

and it works fine!


Well, you managed to use 21 lines of code where 1 would have worked, but as long as it works, that's great. Here's the one line that can replace that all.

textBox2.Text = (newname + 1).ToString("0000");


On to your question.

First, let's consider certain conditions. What happens if something is in that directory that isn't a JPEG? What if someone moves a picture in with a name that's not a number?

If you just want to disregard anything that doesn't match your pattern, that's actually pretty easy. You can use a DirectoryInfo object to point to the proper directory, and it has a GetFiles method that'll let you search for a pattern. Use this to get all .jpg files. Then you can loop through the result, and discard any that don't convert to a number (hint: int.TryParse. Then just sort the remaining, and take the last.

This post has been edited by insertAlias: 22 March 2011 - 09:27 AM

Was This Post Helpful? 0
  • +
  • -

#11 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: [C#] Screenshot

Posted 22 March 2011 - 09:47 AM

You could have also used the PadLeft method...

textBox2.Text = (newname + 1).ToString().PadLeft(3, '0');


Was This Post Helpful? 0
  • +
  • -

#12 rms.x95   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 13-March 11

Re: [C#] Screenshot

Posted 22 March 2011 - 09:53 AM

Thanks!

I had a stupid solution, I see. But you can learn from mistakes like this. I add it to my word document of short useful codes ;)

hmmm, If there are more Jpegs there is a bug, but it is a really easy way :P. also the consequences of the bug is not really big!

Now my project is nearly finished!

I've finished my project!!!

Thanks for helping me!!!

Do I have to mark this topic as solved? If yes, where is the button?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1