Search code examples
c#winformsdrawtobitmap

C# - take picture from inside of a form


OK, I know how DrawToBitmap works or the CopyToScreen methods and all. What I'm looking for is how to take a picture from inside a form (without the form borders, title and maximize box and all) I tried different things for the Rectangle parameter of the DrawToBitmap method but none worked and I just can't figure it out. I appreciate any help.


Solution

  • Well, you have two choices:
    1) Go into complex Windows API's
    2) Take a screenshot and simply cropping out the form.
    1 is probably faster and will work even if something is hiding the form (e.g an other form)
    2 will only work if there's nothing hiding the form, and its in the screen.
    Well, you can use this code if you like:

    Rectangle form = this.Bounds; 
    using (Bitmap bitmap = new Bitmap(form.Width, form.Height)) 
    { 
        using (Graphics graphic = 
            Graphics.FromImage(bitmap)) 
        { 
            graphic.CopyFromScreen(form.Location, 
                Point.Empty, form.Size); 
        } 
        bitmap.Save("D://test.jpg", ImageFormat.Jpeg); 
    }