C#屏幕截取、保存

private Bitmap CaptureScreen1()
        {
            int screenWidth = Screen.PrimaryScreen.Bounds.Width;
            int screenHeight = Screen.PrimaryScreen.Bounds.Height;
            System.Drawing.Rectangle bounds = GetScreenBounds();
            Bitmap bitmap=new Bitmap(bounds.Width, bounds.Height);
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty,bounds.Size);

            }

            return bitmap;
        }
        private Bitmap CaptureScreen2()
        {

//处理分辨率问题
            System.Drawing.Rectangle bounds =Screen.PrimaryScreen.Bounds;

            float dpiX,dpiY;
            using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
            { 
                dpiX = graphics.DpiX / 96f;
                dpiY = graphics.DpiY / 96f;
            }
            int screenWidth = (int)(bounds.Width * dpiX);
            int screenHeight = (int)(bounds.Height * dpiY);
           

  

            Bitmap screenshot = new Bitmap(screenWidth, screenHeight);

            
            using (Graphics graphics = Graphics.FromImage(screenshot))
            {
                graphics.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty,new Size(screenWidth,screenHeight));

            }

            return screenshot;
        }

private Bitmap CaptureScreen3()
        {
           //如果上述两种方法还是无法得到想要的效果,可以采用user32.dll自带的方法
            Size resolution = ScreenResolutionHelper.GetScreenResolution();
            
        

            int screenWidth = resolution.Width;
            int screenHeight = resolution.Height;
       

            Bitmap screenshot = new Bitmap(screenWidth, screenHeight);

           
            using (Graphics graphics = Graphics.FromImage(screenshot))
            {
                graphics.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty,new Size(screenWidth,screenHeight));

            }

            return screenshot;
        }
 

//图片保存

        private void SaveScreenshot(Bitmap screenshot)
        {
            string fileName = "test.png";
            try
            {
                screenshot.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);

            }
            catch (Exception ex)
            {
                MessageBox.Show("Save Failed!" + ex.ToString());
            }
            finally
            {
                screenshot.Dispose();
            }
        }

public static class ScreenResolutionHelper
    {
        [DllImport("user32.dll")]
        private static extern IntPtr GetDesktopWindow();
        [DllImport("user32.dll")]
        private static extern IntPtr GetDC(IntPtr hWnd);
        [DllImport("gdi32.dll")]
        private static extern int GetDeviceCaps(IntPtr hdc,int nIndex);

        [DllImport("user32.dll")]
        private static extern IntPtr MonitorFromWindow(IntPtr hwnd, uint dwFlags);

        [DllImport("user32.dll")]
        private static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFOEX lpmi);


        private const int DESKTOPHORZRES = 118;
        private const int DESKTOPVERTRES = 117;

        private const uint MONITOR_DEFAULTTOPRIMARY = 0x00000001;
        private const int MONITORINFOF_PRIMARY = 0x00000001;

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public class MONITORINFOEX
        {
            public int cbSize;
            public RECT rcMonitor;
            public RECT rcWork;
            public int dwFlags;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string szDevice;
        }

        public static Size GetScreenResolution()
        {
            IntPtr hdc=GetDC(IntPtr.Zero);  
            int horzRes=GetDeviceCaps(hdc, DESKTOPHORZRES);
            int vertRes=GetDeviceCaps(hdc, DESKTOPVERTRES);
            return new Size(horzRes,vertRes);   
        }

        public static Size GetWorkAreaResolution()
        {
            IntPtr primaryMonitor = MonitorFromWindow(GetDesktopWindow(), MONITOR_DEFAULTTOPRIMARY);
            MONITORINFOEX monitorInfo = new MONITORINFOEX();
            monitorInfo.cbSize = Marshal.SizeOf(monitorInfo);
            GetMonitorInfo(primaryMonitor, ref monitorInfo);

            int workAreaWidth = monitorInfo.rcWork.Right - monitorInfo.rcWork.Left;
            int workAreaHeight = monitorInfo.rcWork.Bottom - monitorInfo.rcWork.Top;

            return new Size(workAreaWidth, workAreaHeight);
        }


    }