随笔 - 272  文章 - 7  评论 - 27  阅读 - 83万

计算图片物理大小

复制代码
//先计算系统dpi,以下是代码
public partial class Form1 : Form { int dpi_x; int dpi_y; public Form1() { InitializeComponent(); //SetProcessDPIAware(); //重要 IntPtr screenDC = GetDC(IntPtr.Zero); dpi_x = GetDeviceCaps(screenDC, /*DeviceCap.*/LOGPIXELSX); dpi_y = GetDeviceCaps(screenDC, /*DeviceCap.*/LOGPIXELSY); // dpi_x = 96.0; // dpi_y = 96.0; ReleaseDC(IntPtr.Zero, screenDC); } [DllImport("user32.dll")] static extern IntPtr GetDC(IntPtr ptr); [DllImport("user32.dll", EntryPoint = "ReleaseDC")] public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc); [DllImport("gdi32.dll")] public static extern IntPtr CreateDC( string lpszDriver, // driver name string lpszDevice, // device name string lpszOutput, // not used; should be NULL Int64 lpInitData // optional printer data ); [DllImport("gdi32.dll")] public static extern int GetDeviceCaps( IntPtr hdc, // handle to DC int nIndex // index of capability ); [DllImport("user32.dll")] internal static extern bool SetProcessDPIAware(); const int DRIVERVERSION = 0; const int TECHNOLOGY = 2; const int HORZSIZE = 4; const int VERTSIZE = 6; const int HORZRES = 8; const int VERTRES = 10; const int BITSPIXEL = 12; const int PLANES = 14; const int NUMBRUSHES = 16; const int NUMPENS = 18; const int NUMMARKERS = 20; const int NUMFONTS = 22; const int NUMCOLORS = 24; const int PDEVICESIZE = 26; const int CURVECAPS = 28; const int LINECAPS = 30; const int POLYGONALCAPS = 32; const int TEXTCAPS = 34; const int CLIPCAPS = 36; const int RASTERCAPS = 38; const int ASPECTX = 40; const int ASPECTY = 42; const int ASPECTXY = 44; const int SHADEBLENDCAPS = 45; const int LOGPIXELSX = 88; const int LOGPIXELSY = 90; const int SIZEPALETTE = 104; const int NUMRESERVED = 106; const int COLORRES = 108; const int PHYSICALWIDTH = 110; const int PHYSICALHEIGHT = 111; const int PHYSICALOFFSETX = 112; const int PHYSICALOFFSETY = 113; const int SCALINGFACTORX = 114; const int SCALINGFACTORY = 115; const int VREFRESH = 116; const int DESKTOPVERTRES = 117; const int DESKTOPHORZRES = 118; const int BLTALIGNMENT = 119; private void button1_Click(object sender, EventArgs e) { MessageBox.Show(dpi_x.ToString()+"||"+dpi_y.ToString()); } }
复制代码
 float pw=w/myThumbnail.HorizontalResolution*dpi_x;//物理大小=像素/分辨率*dpi
 float ph=h/2/myThumbnail.VerticalResolution*dpi_y;

 

posted on   NLazyo  阅读(985)  评论(1编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示