2

I want to take a screenshot of a specific part of a website using C# or .net Code, I can't create a Windows form application as it must be embedded into something else.

Here is what I got so far:

using System.Drawing.Imaging;
using System.Drawing;
using System;
using System.Windows.Forms;

try

  {
     //Creating a new Bitmap object

      Bitmap captureBitmap = new Bitmap(1024, 768, PixelFormat.Format32bppArgb);

     //Bitmap captureBitmap = new Bitmap(int width, int height, PixelFormat);
     //Creating a Rectangle object which will  
     //capture our Current Screen

     Rectangle captureRectangle = Screen.AllScreens[0].Bounds;

     //Creating a New Graphics Object

     Graphics captureGraphics = Graphics.FromImage(captureBitmap);

    //Copying Image from The Screen

captureGraphics.CopyFromScreen(captureRectangle.Left,captureRectangle.Top,0,0,captureRectangle.Size);

    //Saving the Image File (I am here Saving it in My E drive).

    captureBitmap.Save(@"E:\Capture.jpg",ImageFormat.Jpeg);

    //Displaying the Successfull Result

    MessageBox.Show("Screen Captured");

}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

This code works fine as long as I am creating a windows form application, but as soon as I change it to console it doesn't work even after adding the "using System.Windows.Forms;" and it produces this error:

//Screen does not exist in the current context

So Is there any solution for this or any other way I could take a screenshot of a specific part of a website Programmatically without connecting to it at all.

Thanks in advance.

Raymond Wu
  • 3,357
  • 2
  • 7
  • 20
yousif fayed
  • 331
  • 1
  • 4
  • 20

1 Answers1

0

I actually found an answer one minute after posting from the suggested links "which btw didn't show in search I did for 2 days for some reason" but anyway

its This question

and its as simple as

Rectangle rect = new Rectangle(0, 0, 100, 100);
Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, 
CopyPixelOperation.SourceCopy);
bmp.Save(fileName, ImageFormat.Jpeg);
yousif fayed
  • 331
  • 1
  • 4
  • 20