0

My goal is to be able to make a bitmap of a window, and just that window. I will also need to be able to make bitmaps of certain pixel regions within that window. These regions are not windows forms or anything like that.

I figured making a screen from the window handle, then writing regions of the screen that I need to a bitmap would be a good solution for this problem.

This is what I did:

 Process[] processlist = Process.GetProcesses();
        String[] process = null;
        process = new string[200];
        int i = 0;
        IntPtr handle = IntPtr.Zero;
        foreach (Process theprocess in processlist)
        {
            process[i] = theprocess.MainWindowTitle;
            if (process[i].Contains("Title of Process"))
            {
                handle = theprocess.MainWindowHandle;
                MessageBox.Show("Handle Set");

                break;
            }
            i++;

        }

        //Sets new screen from Handle

        Screen thescreen = Screen.FromHandle(handle);

        //Bitmap stored
        Bitmap bmpScreenshot = new Bitmap(thescreen.Bounds.Width, thescreen.Bounds.Height);

        //Graphics object to draw screen in bitmap
        Graphics g = Graphics.FromImage(bmpScreenshot);

        //Copy from screen to bitmap
        g.CopyFromScreen(0, 0, 0, 0, thescreen.Bounds.Size);

However, this just gets a bitmap of the entire screen. How can I reduce this to the size of just one window?

Thanks.

Ethan Kershner
  • 121
  • 3
  • 13
  • You have to [pinvoke GetWindowRect()](http://pinvoke.net/default.aspx/user32/GetWindowRect.html) to get the position and size of the window. – Hans Passant Jun 08 '14 at 01:20

1 Answers1

0

You can do this:

Bitmap bmpScreenshot = new Bitmap(this.Bounds.Width, this.Bounds.Height);
DrawToBitmap(bmpScreenshot, this.DisplayRectangle);
pictureBox1.Image = bmpScreenshot;

I tried the above code and it worked fine

Syed Farjad Zia Zaidi
  • 3,302
  • 4
  • 27
  • 50
  • Do I replace "this" with the handle of the window I want to capture? – Ethan Kershner Jun 08 '14 at 00:42
  • Yes, it will display the whole area of the form except the border. – Syed Farjad Zia Zaidi Jun 08 '14 at 00:44
  • When I replace this with the handle of the window I want to screenshot: 'System.IntPtr' does not contain a definition for 'Bounds' and no extension method 'Bounds' accepting a first argument of type 'System.IntPtr' could be found (are you missing a using directive or an assembly reference?' The window I want to capture is not the window of the form the program runs in. – Ethan Kershner Jun 08 '14 at 00:48
  • If you are trying to get the screen of another form you will have to call it like this, form2.DrawToBitmap(bmpScreenshot, s.DisplayRectangle); – Syed Farjad Zia Zaidi Jun 08 '14 at 00:54
  • It is not of a form, but a window completely outside of the program itself. – Ethan Kershner Jun 08 '14 at 00:56
  • http://stackoverflow.com/questions/891345/get-a-screenshot-of-a-specific-application Visit this link you will have your answer... – Syed Farjad Zia Zaidi Jun 08 '14 at 01:10