How to take screenshot of c# application or window?

Generally, if we would like to take screenshot of application , we always use following solution:

var sz = new Size((int) width, (int) height); 
var bmp = new Bitmap(sz.Width, sz.Height); 
                  Graphics.FromImage(bmp).CopyFromScreen((int)left, (int)top, 0, 0, sz);

var filePathName = $"{filePath}/{fileName}.png";
bmp.Save(filePathName);

if(bmp!=null)
{
     bmp.Dispose();
}

However, there is special condition if this code running well. (1) every window visible on the desktop is a Windows Forms window (2) window opacity  is 100% or it is not a layer window.

There is another method named CopyPixelOperation CaptureBlt will solve this kind of problem very well.

As Mentiond,C# CopyPixelOperation CaptureBlt Windows that are layered on top of your window are included in the resulting image.default, the image contains only your window. which meet my requirement. I only would like to screensnap of my current application window not all the windows .

furthermore, we can define the window top and left if we have multiple screen.

you can find sample code on above link , and I will show my code as following.

  public void ScreenPrintAndToDiskUsingGDI(double width, double height, double left, double top, bool )
{
                Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, new Action(delegate
{
     var sz = new Size((int)width, (int)height); 
     IntPtr hDesk = GetDesktopWindow();
     IntPtr hSrce = GetWindowDC(hDesk);
     IntPtr hDest = CreateCompatibleDC(hSrce);
     IntPtr hBmp = CreateCompatibleBitmap(hSrce, sz.Width, sz.Height);
     IntPtr hOldBmp = SelectObject(hDest, hBmp);


    bool b = BitBlt(hDest, 0, 0, sz.Width, sz.Height, hSrce, (int)left, (int)top, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);
   Bitmap bmp = Bitmap.FromHbitmap(hBmp);
   SelectObject(hDest, hOldBmp);
   DeleteObject(hBmp);
   DeleteDC(hDest);
   ReleaseDC(hDesk, hSrce);

  var filePathName = $"{filePath}/{fileName}.png";

bmp.Save(filePathName);

if (bmp != null)
{
    bmp.Dispose();
                       
 }));

 }
}

of course ,we need to do some declarations

        
using System.Runtime.InteropServices;

       // P/Invoke declarations
        [DllImport("gdi32.dll")]
        static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int
        wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, CopyPixelOperation rop);
        [DllImport("user32.dll")]
        static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc);
        [DllImport("gdi32.dll")]
        static extern IntPtr DeleteDC(IntPtr hDc);
        [DllImport("gdi32.dll")]
        static extern IntPtr DeleteObject(IntPtr hDc);
        [DllImport("gdi32.dll")]
        static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
        [DllImport("gdi32.dll")]
        static extern IntPtr CreateCompatibleDC(IntPtr hdc);
        [DllImport("gdi32.dll")]
        static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
        [DllImport("user32.dll")]
        public static extern IntPtr GetDesktopWindow();
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowDC(IntPtr ptr);

Good luck!

Leave a comment