Thursday, May 19, 2011

C# screen capture!

Among the many good things about C# is it's amazing library, the .NET framework.  It may seem almost alien to native programmers, and I'm thoroughly convinced trained monkeys can write C# programs--but here is a small sample of the level of abstraction provided by C# and the rich .NET framework classes.

GetScreen.cs
[file]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;


namespace ScreenCap
{
 
    public class GetScreen
    {
        private Graphics gfx;
        private Bitmap bmp;
        private Point zero;
        private Size resolution;


        public GetScreen()
        {
            zero = new Point(0, 0);
            resolution = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                Screen.PrimaryScreen.Bounds.Height,
                PixelFormat.Format32bppArgb);
            gfx = Graphics.FromImage(bmp);
        }
        ~GetScreen()
        {
            bmp.Dispose();
            gfx.Dispose();
        }
        public Bitmap GetImage()
        {
            gfx.CopyFromScreen(zero, zero, resolution);
            return new Bitmap(bmp);
        }
    }
}


[/file]

Form1.cs
[file]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace ScreenCap
{
    public partial class Form1 : Form
    {
        public GetScreen capture;
        public Form1()
        {
            InitializeComponent();
            capture = new GetScreen();
            //Assigning a snapshot to the window content.
            Graphics gfx = this.CreateGraphics();
            this.Show();//Make sure to sure the window first.
            gfx.DrawImage(capture.GetImage(), this.ClientRectangle);
        }
    }
}
[/file]

If built properly you should see a snapshot of your screen in the window, and a glimpse into the life of the army of trained monkeys writing software for Microsoft Windows.

48 comments:

  1. im more of a cpp guy myself,
    great post, hope to see more

    ReplyDelete
  2. Takes me back to the days of DoS. I do like C++ and visual basic also. But i'm trying some linux these days.

    ReplyDelete
  3. Heh, nice little app. Seems like this would serve as a nice intro to C# (like a "hello windows") lol

    ReplyDelete
  4. I really need to learn to code. This is all Greek to me.

    ReplyDelete
  5. I'm actually gonna fuck around with this...see if i can re-familiarize myself with C a little bit

    ReplyDelete
  6. @Duke,
    I would try adding a method to change the "PixelFormat", and add a method that returns a handle/pointer/reference to the interior "Bitmap"(derived from "Image"), the current one copies and returns a new image.

    Also, you can add a method to adjust to a new Screen resolution, and a method to copy only a portion of the screen.

    You can try adding compression, changing the image format, etc.

    You can add a method to save a screenshot as a jpg with a certain file name too.

    ReplyDelete
  7. Its like I'm in compsci 3/4 all over again.

    ReplyDelete
  8. I should learn how to do such thing. I am still learning the basics of computer programming but it won't take too much time to such thing.

    ReplyDelete
  9. I did something similar to this in my computing coursework a couple years ago. It's bringing back horrible vietnam-like memories.

    ReplyDelete
  10. @DRJT,

    lol that seems to be a common feeling toward programming. I find it to be great fun, myself.

    ReplyDelete
  11. I would have found it fun if I didn't have the worst teacher ever :P

    ReplyDelete
  12. Very nice, going to follow your blog hoping to learn more about the subject.

    ReplyDelete
  13. haven't done any C# in years. Might re-learn it lol.

    ReplyDelete
  14. Interesting read, will keep following your block

    ReplyDelete
  15. You seem to have a lot of knowledge, i've always been interested in learning this sorta stuff, nice post =)

    ReplyDelete
  16. I've been meaning to learn C# for some time now... I think I may take a few weeks and do just that now.

    ReplyDelete
  17. I'd be very happy if you can post an example in C/C++

    ReplyDelete
  18. I have been wanting to learn how to program, never really looked at much besides C++...

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. Need motivation to start learning this again, will follow

    ReplyDelete
  21. @n0kS Phr33d0m,

    An example of screen capture similar to the one described above?

    The brutally gory details of Windows aren't abstracted with C++, and C++ has no standard way to create a window so the project could be written in a multitude of different ways.

    I could,

    1. Use a mixed mode assembly combining native and managed code (it's basically like C# except it probably requires a little more code.) Which would make it super-easy.

    2. Use a GUI library (like QT). Which is already an example showing how to use QT.
    http://doc.qt.nokia.com/4.0/widgets-screenshot.html

    3. Use another Microsoft route, such as MFC or pure Win32. MFC is aging, but it's pretty feature-packed too.

    ReplyDelete
  22. sounds interesting... how long did it take you to learn code?

    ReplyDelete
  23. WoW! it is helpfull, i will use it for sure.

    ReplyDelete
  24. I ended up not dropping a class in c++ in college and ended up failing it because of that. Now I'm going to retake it to erase it off my record and your blog looks like it will help me out with that.

    ReplyDelete
  25. Takes me back to the days of DoS...
    now i'm in C++ and VB

    ReplyDelete
  26. yeah, more of a C++ guy aswell, although, i've never been able to do anything cool with it or get to the stage where i've been able to do what i've wanted.

    ReplyDelete
  27. @adss,

    It is a lifelong commitment to learning, sucks, I know.

    Actually it's fun if you like to learn, and create stuff.

    ReplyDelete
  28. Please be my C+/ hacking teacher!!! followed btw

    ReplyDelete
  29. nice blog!
    i was thinking of basing my blog on programming
    any hints?

    ReplyDelete
  30. These things were always interesting for me. Especially that im gonna start fun with cpp ;)

    ReplyDelete
  31. Nice! Thanks for sharing, interesting as usual.

    ReplyDelete
  32. Nice post lol and microsoft monkeys

    ReplyDelete
  33. C# looks pretty similar to Java. I might switch over!

    ReplyDelete
  34. hm.. i've always sucked at programming. lol

    ReplyDelete
  35. i'm going to study programming hope your blog'll help me to do so :)

    ReplyDelete
  36. How is this more useful than C+? Or is it totally different.

    ReplyDelete
  37. @Admin,

    If by C+ (which doesn't exist), you mean "C++", then I would guess that I don't believe I've claimed it was more useful than C++. For this particular application I have demonstrated the level of abstraction offered by C#.

    If one were to write the same application in C++ one can't expect the language's standard library to include the classes that simplify and greatly abstract the task of capturing a screenshot. It will certainly require more code (work) to write the same application in C++.

    That being said, C++ is still useful, and it's value should be measured by what you use it for. Modern PC games are, for the most part, written in C++, and C++ absolutely shines where performance is critical.

    C++ is also a standardized language, and is still being improved. You might like to check out: http://en.wikipedia.org/wiki/C%2B%2B0x

    With great improvements to a language as popular as C++ brings great benefits to users of software--your operating system, PC games, and system tools all may benefit from the improvements.

    ReplyDelete