Location>code7788 >text

WPF's understanding of device independence

Popularity:500 ℃/2025-03-31 21:58:20
The pixel unit of wpf is 1/96* system dpi. The current system dpi is 96, so the unit length of wpf is 1px pixels. The meaning of this system is how many pixels there are in a physical unit in an inch. For example, the standard 96dpi in Windows means that there are 96 pixels per inch. Then the size of each pixel is 1/96 (inch). The method of calculating dpi is (under the root number (resolution horizontal square + resolution vertical square))/display diagonal dimension (in inches units). For example, a 27-inch 2k display, the calculation of dpi is equal to (under the root number (2560*2560) + (1440*1440))/27. This resolution is 2560 pixels in horizontal direction and 1440 pixels in vertical direction.
Next, we can calculate the physical inch size of a button with With=96 on a system with dpi equal to 96 to 96*(1/96*96)/96=1 (inch). Explain 96* (1/96*96) means how long the button is. Finally, divide by 96 means that the dividing by dpi, and the result is 1 inch. For example, in a system with dpi of 192, the pixel value of a button with a button length of 96* (1/96*192) = 192px, 192(px)/dpi192=1(inch). Although dpi is enlarged, the pixel value of a button with a length of 96 is 192px, but it is divided by 192dpi, and the final result is still 1 inch in physical size.
In general, the device irrelevance of wpf is, my understanding is that no matter what the system resolution is, the Width of the write button is 96, on these different DPI monitors, the physical size and length of this button are still 1 inch. However, if the multiple of dpi is adjusted, such as 120% dpi, the physical size of the button will be longer accordingly.
In winform, there will be such a phenomenon. When it is applied to a system development with dpi of 96, and then placed on a system C with dpi of 85, you will find that the font of the applied system and the length and width of the control have become wider. This is because the control of the a originally wrote Width=96, but on C system, only 85 pixels per inch is needed, so 96/85>1 inch is gone, so it looks longer. Similarly, if you place it on a system M with dpi of 192, you will find that the length and width of the control have doubled the size of the font, because 96/192=0.5 (inch), it is only 0.5 inches long, half of it.
This is also why Windows has the function of amplifying the dpi, which is enlarged at 120%. This enlargement does not mean that dpi*1.2 is enlarged, but the size of the enlarged pixel points. After enlarging, the pixel points required to represent 1 inch will be reduced because the pixel points are enlarged. Then, the 96 standard dpi software will be extended under the 120% enlargement effect of the high-dpi system, because the edges of the pixel points are larger, so that the font and control sizes are closer to the size of the 96dpi system.