highdpi - 图标大小在更高 DPI 中没有增加
问题描述
我在 DPI 大于 100 时遇到了这个问题。我更改了图标的大小,例如粗体、斜体,当我以 100 dpi 运行程序时,图标大小更大,但是当我在更改为 dpi 后运行程序时大于 100 图标越来越小,并且没有更新到任何大小值。我试过 autosize = false,图像缩放到无。
解决方案
使用“System.Drawing.Icon”图标时,如果您使用大于 100 的 DPI,您应该记住使用更大尺寸的图标。属性autosize在这里没有帮助。
图标的文件可以包含不同的大小,因此我们可以检测实际的 DPI 比例因子并考虑这个因子以从文件系统加载具有适当大小的图标。
检测 DPI 因子的代码如下所示:
using System;
using System.Drawing;
using System.Runtime.InteropServices;
public static class DpiHelper
{
private static readonly double m_dpiKoef = Graphics.FromHdc(GetDC(IntPtr.Zero)).DpiX / 96f;
public static double GetDpiFactor()
{
return m_dpiKoef;
}
[DllImport("User32.dll")]
private static extern IntPtr GetDC(IntPtr hWnd);
}
现在使用System.Drawing.Icon 中的Icon(string fileName, int width, int height)图标的新实例的初始化可能如下所示:
int size = 48;
int dpiSize = (int)(size * DpiHelper.GetDpiFactor());
Icon dpiIcon = new Icon(filename, new Size(dpiSize, dpiSize));
推荐阅读
- laravel - 在laravel导入中验证excel文件时不显示消息
- javascript - URL 中的参数获取树中的第一个子节点,而不是选定的 Javascript
- google-apps-script - 如何使用 Google Apps 脚本将有关频道的所有视频的信息获取到 Google 表格
- android - 一段时间后,前台服务被Android杀死
- python - 如何使用 Matplotlib.basemap 创建表示纬度和经度计算值的彩色地图
- ios - 在 iOS 中播放原始 PCM 数据缓冲区时音频失真
- sql-server - 使用 LIKE 的 SQL 视图?
- pandas - 在 pandas / Python 中更有效地替换特定行的值
- node.js - 如何在 Express 应用程序中通过 SSL 获取 websocket?
- pandas - Python Pandas:如何删除具有特定“对象”数据类型值的行?