How to get a portion of a screenshot with copyfromscreen

I am trying to create a small application that allows me to cut out a piece of the full screenshot. Right now I create a new bitmap and specify it with the mouse coordinates.

        Bitmap bitmap = new Bitmap(e.X, e.Y);
        Graphics g = Graphics.FromImage(bitmap);
        g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
        pictureBox1.Image = bitmap;
        bitmap.Save(@"C:\Users\asus\Desktop\test c#\test.png");

When I use this code and click on my screen shown here http://prntscr.com/ficotc I get a screenshot of that piece and that works fine but only if I start from the left top corner. If I want to start somewhere else for example in the middle of the screen I change this.

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

to something like this

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

Then it starts 100 pixels from the top and left side but also ends up with 100 more pixels on the right and bottom side which makes the picture go bigger then where I clicked this is the output I will receive and you can see where I clicked but it shows more on the right and bottom side then I want it to. http://prntscr.com/ficq86

I also tried changing the 2 other parameters but resulted in no luck I don't exactly know what it does can anyone tell me what I am doing wrong?


Solution 1:

According to docs, last parameter is size of rectangle to capture. try to reduce its size.

 var size = new Size(Screen.PrimaryScreen.Bounds.Size.Height - 100,
                      Screen.PrimaryScreen.Bounds.Size.Width -100);
 g.CopyFromScreen(100, 100, 0, 0, size);