List of Anonymous types in C#

The given link provides a very simple and easy approach for defining and using a list of anonymous types. http://kirillosenkov.blogspot.com/2008/01/how-to-create-generic-list-of-anonymous.html
This saves a lot of trouble of defining a new type for just onetime usage.

Advertisements

Capturing Screen as Bitmap Image in C#

The easiest way for taking screen snapshot in C# is given in the following code.

            Rectangle workingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
            Bitmap screenShot = new Bitmap(workingArea.Width, workingArea.Height);

            Graphics gr = Graphics.FromImage(screenShot);

            gr.CopyFromScreen(0, 0, 0, 0, workingArea.Size);

            return screenShot;

In the above code, first a rectangle is created which equal to the size of the primary screen.
Then a bitmap is created which will store the pixels for the snapshot image.
The Graphics object is created which will store and perform operations on the bitmap object we just created.
The CopyFromScreen function captures the screen, copying all the color pixels to the bitmap of the Graphics object. The parameters to the function are about from where the copying will start in the source and to where it will go in the target. The first two are x and y are starting coordinates for the source and the third and fourth are about the starting coordinates for the target location. The last parameter is about the size of the rectangular region which will be copied, which consist of width and height.