This article is a personal blog backup article, the original address:
http://validvoid.net/win2d-choosing-control-resolution/
This article is intended to explain how to configure the resolution used by win2d XAML controls. The following sections describe how to:
- Causes the win2d control to run at a fixed resolution.
- Increase performance by reducing the number of rendered pixels by adjusting the control DPI.
resolution and control scaling
The term "resolution" used in this article refers to the size of the bitmap (aspect).
Win2d XAML controls draw objects that have both resolution and DPI properties. The DPI of the object indicates the density of its constituent pixels when drawn. DPI is like scaling factor--dpi the higher the number of pixels the object is drawn in, the lower the DPI, the less it is composed of pixels. For more information about the general win2d for DPI processing, you can refer to another article.
DPI-independent dimensions are sometimes referred to as "logical dimensions." DPI-related dimensions, which are dimensions in pixels, are also referred to as "physical dimensions."
The default behavior for the resolution and scaling of a control at load time is:
- Controls determine their logical dimensions based on their layout and their location in the XAML element tree.
- The control determines its own DPI based on the DPI value of the system's operating environment.
- The control determines the number of physical pixels of its plotted region, based on the size of the control after DPI scaling.
- At high DPI, the physical size will be greater than the logical size (more pixels).
- At low DPI, the physical size is smaller than the logical size (less pixels).
- By default DPI, the physical dimensions of the plotted area are equal to the logical dimensions.
- Control sets the size and DPI of its drawing resources (
CanvasControl
controls CanvasImageSource
, controls, CanvasVirtualControl
CanvasVirtualImageSource
and CanvasAnimatedControl
controls CanvasSwapChain
) to match itself.
Most win2d operations are based on DIPs (DPI-independent units), and the drawing resources for win2d XAML controls are automatically scaled based on DPI. This means that the usual application can ignore DPI. Dimensions and coordinates are always DPI-independent unless otherwise specified. An app can hard-code many different dimensions and coordinates for the content that is drawn to the control, and the content is scaled when the app is running in a different DPI environment.
But for some applications, this default behavior is not enough. This article outlines several scenarios where the default behavior is inadequate and explains how the application can correct it.
Scenario: The resolution of the control content must be fixed and less than normal
This is exactly what is required when a 2D sprite (sprite) game needs to ignore the actual resolution of the display hardware, which is always rendered at a fixed 640x480 resolution.
There is absolutely no need to write any additional win2d code to address this requirement.
The XAML control ViewBox is able to force the size change of its child elements, which are scaled automatically by applying the mailbox mode (adding margins up or down the content) or posting mode (adding margins on both sides of the content) to maintain the aspect ratio.
Just make sure your CanvasControl
, CanvasVirtualControl
or CanvasAnimatedControl
control is set to a ViewBox
child element of a control, and its size is limited by it.
For example, to override the DPI and force the control to be 320 pixels wide and set to 224 pixels high, the
< Canvas:canvasanimatedcontrol />
Revision changed to
< Viewbox > < Canvas:canvasanimatedcontrol Width = " the" Height = "224" /> </ Viewbox >
If you don't want your app to maintain content proportions by using mailbox Mode/Mailbox mode, you'll need to add Stretch
attributes:
<Stretch= "Fill"> <Width= " " Height=" 224 "/></Viewbox>
NoteIf you use Viewbox to scale a control, its interpolation mode does not guarantee the zoom effect. The filtering method may look like a CanvasInterpolationMode.Linear
similar method. If one of your uses requires a certain interpolation pattern, do not use ViewBox to fix the size of the control, but instead draw the content to a fixed size CanvasRenderTarget
as a transit, and then draw to the target with the desired interpolation mode scaling.
Scenarios: Applications render erratically in high-resolution environments
Some display devices have very high resolution, but their GPU (graphics processing unit) does not perform well enough to smooth the animation of large numbers of pixels. Without testing, it is difficult for developers to know how to perform on such high-resolution devices. One solution is to use the properties of the control to DpiScale
reduce the DPI of the control.
For example, use the following code to halve the resolution of the control:
<dpiscale= "0.5f"/>
In real-world use, the DPI scaling factor depends on your application's specific needs. One approach is to fix the scaling factor to ensure that the DPI of your app is always 96 rather than higher.
For example:
float 96.0f ; if (Control. Dpi > dpilimit) { = dpilimit/ control. Dpi;}
To ensure that this practice takes effect when DPI changes, the app subscribes to the Displayinformation.dpichanged event and calculates and sets the dpi scaling factor in the time-processing logic at the changed DPI.
This approach is in exchange for partial performance gains by leveraging the less noticeable resolution in high DPI environments.
Similar to the ViewBox mentioned above, the interpolation mode of the method of narrowing the resolution of the control does not guarantee the zoom effect of the control. If your application requires a specific interpolation pattern, you need to switch to a drawing middleware instead.
WIN2D Official Article Series translations-adjust control resolution