c#抓屏功能在DPI缩放后,截到的图片不完整的问题

Posted on 2017-04-26 15:32  Rock Wang  阅读(2508)  评论(0编辑  收藏  举报
        /// <summary>
        /// 获取屏幕快照
        /// </summary>
        /// <returns></returns>
        public static Bitmap GetScreenSnapshot()
        {
            Bitmap bitmap = null;
            try
            {
                Graphics g = Graphics.FromHwnd(IntPtr.Zero);
                //100%的时候,DPI是96;这条语句的作用时获取放大比例
                float factor = g.DpiX / 96;
                Rectangle rc = new Rectangle(0, 0, (int)(SystemParameters.PrimaryScreenWidth * factor), (int)(SystemParameters.PrimaryScreenHeight * factor));
                bitmap = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb);
                using (Graphics memoryGrahics = Graphics.FromImage(bitmap))
                {
                    memoryGrahics.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy);
                }
            }
            catch (Exception ex)
            {
                NlogHelper.Error("截屏失败", ex);
            }
            return bitmap;
        }