C#:涉及DPI的高分辨率下的顯示問題


一、背景

  在PC機上顯示正常,在高分辨率下的Pad上,顯示出現問題:

    1、顯示在屏幕最右端的窗體(控件)顯示不出來;

    2、截圖時,被截圖的界面字體文字變大,界面因此顯示不全。

二、解決方法:

  方法一:WPF上使用WPF方式獲取屏幕大小,而不是Winform的獲取屏幕大小的方式。

                //Size primarySize = Screen.PrimaryScreen.Bounds.Size;
                double dWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
                double dHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
View Code

  方法二:Winform解決方法:

  1、設置窗體的背景圖片方式改為可縮放方式(Zoom): BackgroundImageLayout = ImageLayout.Zoom;

  2、依據DPI擴展拷貝圖片的大小,設置拷貝的圖片的DPI(bmp的SetResolution方法)

BackgroundImage = GetDestopImage();
BackgroundImageLayout = ImageLayout.Zoom;



        private Image GetDestopImage()
        {
            float rate = dpi / 96;
            Rectangle rect = Screen.GetBounds(this);
            Size sz = new System.Drawing.Size();
            sz.Width = (int)(rect.Size.Width * rate);
            sz.Height = (int)(rect.Size.Height * rate);
            Bitmap bmp = new Bitmap(
                sz.Width, sz.Height, PixelFormat.Format32bppArgb);
            bmp.SetResolution(dpi, dpi);
            Graphics g = Graphics.FromImage(bmp);
            g.CopyFromScreen(0, 0, 0, 0, sz);
            //IntPtr gHdc = g.GetHdc();
            //IntPtr deskHandle = NativeMethods.GetDesktopWindow();

            //IntPtr dHdc = NativeMethods.GetDC(deskHandle);
            //NativeMethods.BitBlt(
            //    gHdc,
            //    0,
            //    0,
            //    Width ,
            //    Height,
            //    dHdc,
            //    0,
            //    0,
            //    NativeMethods.TernaryRasterOperations.SRCCOPY);
            //NativeMethods.ReleaseDC(deskHandle, dHdc);
            //g.ReleaseHdc(gHdc);
            //bmp.Save("test.png");
            return bmp;
        }
View Code

  3、修改拷貝內容位置信息

        private void DrawLastImage()
        {
            float rate = dpi / 96;
            int reWidth = (int)(Width * rate);
            int reHeight = (int)(Height * rate);
            using (Bitmap allBmp = new Bitmap(
                reWidth, reHeight, PixelFormat.Format32bppArgb))
            {
                allBmp.SetResolution(dpi,dpi);
                using (Graphics allGraphics = Graphics.FromImage(allBmp))
                {
                    allGraphics.InterpolationMode = 
                        InterpolationMode.HighQualityBicubic;
                    allGraphics.SmoothingMode = SmoothingMode.AntiAlias;
                    allGraphics.DrawImage(
                        BackgroundImage,
                        Point.Empty);
                    DrawOperate(allGraphics);
                    allGraphics.Flush();

                    Rectangle reSelectImageRect = new Rectangle();
                    reSelectImageRect.X = (int)(SelectImageRect.X * rate);
                    reSelectImageRect.Y = (int)(SelectImageRect.Y * rate);
                    reSelectImageRect.Width = (int)(SelectImageRect.Width * rate);
                    reSelectImageRect.Height = (int)(SelectImageRect.Height * rate);
                    Bitmap bmp = new Bitmap(
                       reSelectImageRect.Width,
                       reSelectImageRect.Height,
                       PixelFormat.Format32bppArgb);
                    bmp.SetResolution(dpi, dpi);
                    Graphics g = Graphics.FromImage(bmp);
                    g.DrawImage(
                        allBmp,
                        0,
                        0,
                        reSelectImageRect,
                        GraphicsUnit.Pixel);

                    g.Flush();
                    g.Dispose();
                    _image = bmp;
                }
            }
        }
View Code

 

  4、獲取DPI代碼:

        public static float getLogPiex()
        {
            float returnValue = 96;
            try
            {
            RegistryKey key = Registry.CurrentUser;
            RegistryKey pixeKey = key.OpenSubKey("Control Panel\\Desktop");
            if (pixeKey != null)
            {
                var pixels = pixeKey.GetValue("LogPixels");
                if (pixels != null)
                {
                    returnValue = float.Parse(pixels.ToString());
                }
                pixeKey.Close();
            }
            else
            {
                pixeKey = key.OpenSubKey("Control Panel\\Desktop\\WindowMetrics");
                if (pixeKey != null)
                {
                    var pixels = pixeKey.GetValue("AppliedDPI");
                    if (pixels != null)
                    {
                        returnValue = float.Parse(pixels.ToString());
                    }
                    pixeKey.Close();
                }
            }
            }
            catch(Exception ex)
            {
                returnValue = 96;
            }
            return returnValue;
        }
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM