Let's get a few ideas straight:
- DPI :dots per inch, points in inches. We often say that the mouse dpi, refers to the mouse to move an inch away from the distance of the number of points, print dpi, the length of each inch of printing points, scanning dpi, scanning per inch how many points. (For more information, please refer to Baidu Encyclopedia http://baike.baidu.com/view/49853.htm)
- Pixels:abbreviations for pixel,picute and element. Pixels can be simply understood as dots inside the dpi. For example, the display has a resolution of 1024 pixels *768 pixels, which means that the display can display 1024 dots (pixels) horizontally, and the longitudinal scientific research can display 768 dots (pixels). Some monitors each display point arrangement of relatively close, 1 inches of the length can be arranged in more points, some of the more loose arrangement, point less, so there is no direct relationship between pixels and inches. (For more information, please refer to Baidu Encyclopedia http://baike.baidu.com/view/575.htm)
- Resolution: above, but we often say adjust the resolution of the display, what is the meaning of it? Display has a natural resolution, that is, the maximum capability of the display, for example, the natural resolution of the display is 1600*1200, then the length of less than 1600, the width of less than 1200 resolution can be displayed, 1900*1300 such a resolution is not.
- Set Custom Text Size (DPI) : WIN7 This dpi I personally think, and the above DPI is not the same thing, do not know why Microsoft put this stuff also called DPI. First, the physical DPI of the display device is unlikely to change; second, friends find that the default value for this DPI is 96, which is 1 inches to print 96 pixels, and when we adjust the DPI to 125, 1-inch prints 125 dots. Most of our application interface is designed according to 96DPI, after 125DPI, the length of 1 inches is no longer 1 inches, should be smaller, in fact, not smaller, but bigger, so, I think and the above DPI is not the same thing, at least not change the physical DPI
WPF units
The WPF window and all elements within it are measured using "stand-alone device units". A standalone device unit is defined as one-inch 96 points. To understand what it means in practical applications, consider an example.
Suppose you create a 96x96 unit-size button in WPF, and if you are using the standard Windows DPI setting (96DPI), then each individual device unit corresponds to an actual physical pixel. This is because WPF uses the following calculation methods:
[Physical unit size]=[standalone device unit size]x[system dpi points]
=1/96 inch * 96 dots per inch
= 1 pixels
In essence, WPF assumes that using 96 elephants to make up an inch is obtained through the settings of the system DPI of Windows. In any case, the actual value depends on your display device.
For example, a 20-inch LCD monitor has a maximum resolution of 1600x1200 pixels. The pixel density of this display can be calculated by the following simple calculation.
[System dots per inch]=sqrt (1600x1600 +1200x1200) pixels/19 inches (Sqrt is open root)
= 100 dots per inch
The pixel density in this case is 100dpi, which is actually higher than the value assumed by Windows. As a result, the display's buttons are smaller than an inch in 96x96 pixels.
On the other hand, the 15-inch LCD display has a resolution of 1024x768 and its pixel density drops to about 85dpi per inch, so the 96x96 button is actually bigger than an inch.
In both cases, if you reduce the screen size (choose 800x600 resolution), the button will be proportionally larger because the system's DPI setting is still 96dpi. In other words, Windows still assumes that the 96 pixel size is used as an inch. Even at lower resolutions, the number of pixels has been much less than in many cases.
system DPI
So far, how the button works for WPF and other user interface elements in the application work is strictly consistent. The difference is the result when you change the DPI setting of the system. In earlier Windows, this feature was occasionally referred to as the "big font". This is because the system DPI affects the size of the system font, but other details are not changed.
This is exactly where WPF differs. WPF considers system-specific DPI settings, and if you change the system DPI to 120DPI,WPF it will assume that it requires 120 pixels to fill an inch of space. WPF uses the following calculations to figure out how to convert a logical unit to a pixel of a physical device.
[Physical unit size]= [standalone device unit size]x[system DPI]
=1/96 inch x120 dpi
=1.25 pixels |
In other words, when you set the system DPI to 120dpi, the WPF rendering engine assumes that a separate device unit equals 1.25 pixel size. If a 96x96 button is displayed, the physical size is actually 120 pixels x120 pixels, which is the desired result-a button that appears as an inch in the standard display and still appears as an inch in the higher pixel density display.
This auto-scaling feature is not really useful if it is only applied to the button. But WPF uses stand-alone device units to display everything, including shapes, controls, text, and any elements that are placed in the form. As a result, the DPI of the system can be changed to any desired value, and WPF automatically adjusts the size of the application seamlessly. How to change the DPI depends on the system you are using, and the steps are not detailed.
bitmap and vector graphics
When you use normal controls, you can take advantage of the independence of the resolution of WPF . WPF automatically focuses on all controls to ensure that they all have the correct dimensions. However, if you want to merge the images into your application, you can't be careless. For example, in a traditional application, a developer uses a small bitmap to a toolbar directive. In a WPF application, this is not ideal because the bitmap may appear blurry depending on the system's DPI setting and zooming in and out. Instead, when you design the WPF application interface, even the smallest icons are usually designed as vectors. Vectors are defined as a set of graphs, and because of this, they can be scaled to any size.
It is difficult to overestimate the importance of resolution independence. At first glance it seems to be a simple (straightforward) first-class (elegant) solution that solves a long-standing problem. However, in order to design the user interface can be flexible. Developers need a new way of thinking.
--===================================== Gorgeous delimiter ===============================================
The units in a WPF program are device-independent units, each unit is 1/96 inches, and if the computer's DPI is set to 96 (96 pixels per inch), then each WPF unit corresponds to one pixel at a time, but if the computer has a DPI device of 120 (120 pixels per inch), That at this time each WPF unit corresponds to a 120/96=1.25 pixel
Generally in the program we often need to get the current screen width and height, common practices are:
1.system.windows.forms.screen.primaryscreen.bounds.width
System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height
These two methods can return the resolution of the current screen selection, which is in pixels and we can use them to do some positioning of the controls at DPI 96, because the WPF unit corresponds to a pixel, and when the DPI is not 96, the resolution is used to locate the error. Therefore, each WPF unit does not correspond to one pixel at this time
2.systemparameters.primaryscreenwidth
Systemparameters.primaryscreenheight
Both methods can return the width and height of the current screen, which is device-independent (1/96-inch), so it is used to position the control, and in the case of DPI change, there will be no positioning error.
3.systemparameters.workarea.size.width
SystemParameters.WorkArea.Size.Height
These two methods can return the width and height of the current screen's workspace (except for the taskbar), which is also device-independent, and usually we can combine 2 and all to get the height of the taskbar
--===================================== Gorgeous delimiter ===============================================
is the WPF unit really independent of resolution?
WPF has been highlighting "resolution-independent (resolution Independence)" since its release, claiming that the WPF-crafted user interface is well-displayed on a lightweight Ultra-Mobile PC screen and on a 50-inch TV. Microsoft says that WPF has a "resolution independent" feature, mainly because the coordinate units of WPF are designed to be in 1/96-inch units of logical pixels rather than device-related units of pixels.
But Microsoft itself does not have a more specific explanation of WPF's "Resolution-independent" nature, resulting in many misunderstandings.
Misunderstanding one
Changing the resolution settings of the display does not change the user interface and drawing size of the same WPF.
This can be proved by a very simple experiment that the conclusion is wrong. Create a new WPF application window with a height of 400DIUs(Diu:device Independent Unit, device independent unit)and a width of 600DIUs to allow this window to be set at resolution 1280 * 1024 and 800* respectively 600 of the environment, as shown, the size of the two windows is obviously different.
misunderstanding of the second
Changing the DPI setting for the display will not change the user interface and drawing dimensions of the same WPF.
The DPI setting displayed in the XP system is through the right---Properties--Settings tab--Advanced, which can be called out, as shown in:
Figure 2 Displaying the properties DPI setting
This can also be used in the same way to prove that the conclusion is wrong. Still height is 400dius[1], windows with a width of 600DIUs respectively run in 96DPI and 192DPI two settings environment. It is also evident that the size of the window is not the same.
misunderstanding of the third
On different screens, if the DPI setting is the same, the user interface and drawing dimensions of the same WPF do not change.
In this place it is necessary to explain the DPI setting of the screen. The DPI setting is the number of pixels per inch on the screen, such as the current setting of 96DPI, which means that 96 pixels on the screen are 1 inches. Typical Windows XP systems have three options for normal size (96DPI), large size (120DPI), and custom size. Since the coordinate units of WPF are in 1/96-inch units of logical pixels, we have reason to believe that if the DPI setting of the two monitors is the same, the user interface and the drawing size of the same WPF will not change. Unfortunately, such a conclusion is still a mistake.
Calvinp.schrotenboer also used an experiment to prove that this was a mistake. The experimental environment such as table 1, such as the actual screen width and height of the desktop LCD display (pixel units) is 1200, this and ordinary resolution settings need to distinguish, this is the maximum resolution of the display device or the physical resolution, that is, physically on the display screen is a five x 1200 cells, This resolution is also known in English as "native resolution (native resolution)". Because the physical size of the two screens is not the same, the actual physical DPI can be obtained from the calculation formula in the table. There is no connection between the actual physical DPI and the DPI setting of the operating system.
Table 1 Experimental environment
|
Experimental environment |
System One |
System Two |
|
Display Type |
Desktop LCD display |
Laptop LCD Monitor |
|
Screen width and height
(pixel units) |
1200 x |
1400 x 1050 |
|
Screen width and height
(inch units) |
17.0 x 12.75 |
12.0 x 9.0 |
|
The actual physical DPI |
Portrait: 1600/17.0 = 94DPI
Transverse: 1200/12.75 = 94DPI |
Portrait: 1400/12 = 117DPI
Transverse: 1050/9 = 117DPI |
|
DPI settings for the operating system |
96DPI |
96DPI |
Running the same WPF application in two different systems, the program draws a line with a length of 384DIUs, which translates into inches of 384/96= 4 inches. The actual size of the results in both systems is as follows:
Where's the problem?
In fact, you can see some clues from table 1, the reason is that the actual physical DPI and operating system settings dpi inconsistency caused. WPF does not know how much of the actual physical DPI is currently using the device, but instead obtains the operating system's DPI value through the operating system's API functions, and then simply considers this to be the actual physical DPI value. For example, on a desktop LCD display, the actual size of a physical cell is 1/94 inches, and because the operating system is set to 96DPI, WPF also stubbornly believes that an actual cell is 1/96 inches, so the segment length is 1/94 * 384 = 4.08 inches. The notebook display actually has a physical cell size of 1/117 inches, so the segment length is 1/117 * 384 = 3.28 inches. This value coincides with the result of our measurement.
Then we have reason to speculate that if the DPI of the operating system is set to the actual physical dpi, then you can achieve true "resolution independent", that is, the length of the segment displayed on two different monitors is 4 inches , 5 is as follows:
The "Resolution-independent" of WPF has so far been a mountain high month small, to the bottom. So we're going to go on to another question, if there's such a problem on the monitor, is there a problem with the printer? Can still be proved by an experiment. Also draw a 4-inch line that prints at a DPI setting of 96DPI and 120DPI, resulting in the same size. As shown in the following:
Conclusion
Through the above experimental analysis, we can get the following two conclusions:
(1) WPF can be "resolution-independent" when printing, that is, the same WPF user interface and drawing the graphics size on any one printer output is consistent;
(2) When the physical dimensions of the display's actual cell and the DPI of the system settings are consistent, WPF can do "resolution-independent" on the display, That is, the output of the same WPF user interface and drawing dimensions is consistent on any one display (the physical size of the actual cell and the DPI of the system setting). Conversely, there is no guarantee.
More discussion
The concept of "resolution-independent", because Microsoft itself is not much discussed, is likely to cause misunderstanding. The most detailed discussion of "Resolution-independent" in WPF is a blog post "is WPF really Resolution independent?" Calvinp.schrotenboer. Charles Peztold, of course, has also discussed this issue in his blog. In addition, in Microsoft's Forum Stephenw,charles Peztold and others also made a more in-depth discussion on WPF's "Resolution-independent" and "device-independent".
Users can ignore these, but for a programmer, especially a programmer who is working on a drawing program, especially a drawing programmer who also needs to print out, needs to know the details. And it's a pleasure to know the details themselves.
Reference documents
Http://www.cnblogs.com/xiaokang088/archive/2011/03/02/1969237.html
Http://www.cnblogs.com/helloj2ee/archive/2009/04/21/1440709.html
Http://www.cnblogs.com/dayrain/archive/2008/11/02/1324870.html
Issues with DPI in WPF