CopyFromScreen在屏幕缩放情况下需要做处理

Posted ZGJ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CopyFromScreen在屏幕缩放情况下需要做处理相关的知识,希望对你有一定的参考价值。

using System;
using System.Drawing;
using System.Runtime.InteropServices;
//这段代码转自网上
namespace System.Util
{
    public class PrimaryScreen
    {
        #region Win32 API  
        [DllImport("user32.dll")]
        static extern IntPtr GetDC(IntPtr ptr);
        [DllImport("gdi32.dll")]
        static extern int GetDeviceCaps(
        IntPtr hdc, // handle to DC  
        int nIndex // index of capability  
        );
        [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
        static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
        #endregion
        #region DeviceCaps常量  
        const int HORZRES = 8;
        const int VERTRES = 10;
        const int LOGPIXELSX = 88;
        const int LOGPIXELSY = 90;
        const int DESKTOPVERTRES = 117;
        const int DESKTOPHORZRES = 118;
        #endregion

        #region 属性  
        /// <summary>  
        /// 获取屏幕分辨率当前物理大小  
        /// </summary>  
        public static Size WorkingArea
        {
            get
            {
                IntPtr hdc = GetDC(IntPtr.Zero);
                Size size = new Size();
                size.Width = GetDeviceCaps(hdc, HORZRES);
                size.Height = GetDeviceCaps(hdc, VERTRES);
                ReleaseDC(IntPtr.Zero, hdc);
                return size;
            }
        }
        /// <summary>  
        /// 当前系统DPI_X 大小 一般为96  
        /// </summary>  
        public static int DpiX
        {
            get
            {
                IntPtr hdc = GetDC(IntPtr.Zero);
                int DpiX = GetDeviceCaps(hdc, LOGPIXELSX);
                ReleaseDC(IntPtr.Zero, hdc);
                return DpiX;
            }
        }
        /// <summary>  
        /// 当前系统DPI_Y 大小 一般为96  
        /// </summary>  
        public static int DpiY
        {
            get
            {
                IntPtr hdc = GetDC(IntPtr.Zero);
                int DpiX = GetDeviceCaps(hdc, LOGPIXELSY);
                ReleaseDC(IntPtr.Zero, hdc);
                return DpiX;
            }
        }
        /// <summary>  
        /// 获取真实设置的桌面分辨率大小  
        /// </summary>  
        public static Size DESKTOP
        {
            get
            {
                IntPtr hdc = GetDC(IntPtr.Zero);
                Size size = new Size();
                size.Width = GetDeviceCaps(hdc, DESKTOPHORZRES);
                size.Height = GetDeviceCaps(hdc, DESKTOPVERTRES);
                ReleaseDC(IntPtr.Zero, hdc);
                return size;
            }
        }

        /// <summary>  
        /// 获取宽度缩放百分比  
        /// </summary>  
        public static float ScaleX
        {
            get
            {
                IntPtr hdc = GetDC(IntPtr.Zero);
                int t = GetDeviceCaps(hdc, DESKTOPHORZRES);
                int d = GetDeviceCaps(hdc, HORZRES);
                float ScaleX = (float)GetDeviceCaps(hdc, DESKTOPHORZRES) / (float)GetDeviceCaps(hdc, HORZRES);
                ReleaseDC(IntPtr.Zero, hdc);
                return ScaleX;
            }
        }
        /// <summary>  
        /// 获取高度缩放百分比  
        /// </summary>  
        public static float ScaleY
        {
            get
            {
                IntPtr hdc = GetDC(IntPtr.Zero);
                float ScaleY = (float)(float)GetDeviceCaps(hdc, DESKTOPVERTRES) / (float)GetDeviceCaps(hdc, VERTRES);
                ReleaseDC(IntPtr.Zero, hdc);
                return ScaleY;
            }
        }
        #endregion
    }
}

 this.BackColor = Color.Black;
            Rectangle rect1 = parentControl.ClientRectangle;
            this.SetBounds(rect1.Left, rect1.Top, rect1.Width, rect1.Height);
            this.Parent = parentControl;
            Double scaleX = PrimaryScreen.ScaleX;
            Double scaleY = PrimaryScreen.ScaleY;
            if (this.BackgroundImage == null)
            {
                Rectangle rect = parentControl.ClientRectangle;
                Rectangle sRect = parentControl.RectangleToScreen(rect);
                Bitmap bit = new Bitmap((int)(sRect.Width* scaleX), (int)(sRect.Height* scaleY));//实例化一个和窗体一样大的bitmap
                Graphics g = Graphics.FromImage(bit);
                g.CompositingQuality = CompositingQuality.HighQuality;//质量设为最高
                int width = (int)(this.Parent.Parent.Width* scaleX);
                int height = (int)(this.Parent.Parent.Width * scaleX);
                g.CopyFromScreen((int)(sRect.Left* scaleX), (int)(sRect.Top * scaleY), 0, 0, new Size((int)(this.Width * scaleX), (int)(this.Height * scaleY)));
                //g.CopyFromScreen(0, 0, 0, 0, new Size((int)(width), (int)(height)));
                this.BackgroundImage = bit;
                this.BackgroundImageLayout = ImageLayout.Stretch;
                this.BringToFront();
            }
            bShow = true;

 

以上是关于CopyFromScreen在屏幕缩放情况下需要做处理的主要内容,如果未能解决你的问题,请参考以下文章

如何在正确保持纵横比的情况下进行缩放

如何在不使用 UIImageView 的情况下捏缩放 UIImage?

WPF 获取元素(Visual)相对于屏幕设备的缩放比例,可用于清晰显示图片

如何在不实际缩放的情况下更改视图缩放值?

Qt之使用GDI实现屏幕快速抓图与缩放

在C#中如何实现winform窗体的全屏截图功能