Skip to content

Commit d8f946e

Browse files
Merge pull request #3727 from MicrosoftDocs/alvinashcraft-update-pre-blocks-md
Update pre blocks to use markdown and tweak title to be unique
2 parents 02c2018 + 3402b46 commit d8f946e

File tree

1 file changed

+31
-35
lines changed

1 file changed

+31
-35
lines changed

uwp/app-resources/images-tailored-for-scale-theme-contrast.md

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,40 @@
11
---
22
description: Your app can load image resource files containing images tailored for display scale factor, theme, high contrast, and other runtime contexts.
3-
title: Load images and assets tailored for scale, theme, high contrast, and others
3+
title: Load images and assets tailored for scale, theme, high contrast, and more
44
template: detail.hbs
5-
ms.date: 10/10/2017
5+
ms.date: 08/15/2023
66
ms.topic: article
7-
keywords: windows 10, uwp, resource, image, asset, MRT, qualifier
7+
keywords: windows 10, windows 11, uwp, resource, image, asset, MRT, qualifier
88
ms.localizationpriority: medium
99
---
10-
# Load images and assets tailored for scale, theme, high contrast, and others
11-
Your app can load image resource files (or other asset files) tailored for [display scale factor](/windows/apps/design/layout/screen-sizes-and-breakpoints-for-responsive-design), theme, high contrast, and other runtime contexts. These images can be referenced from imperative code or from XAML markup, for example as the **Source** property of an **Image**. They can also appear in your app package manifest source file (the `Package.appxmanifest` file)—for example, as the value for App Icon on the Visual Assets tab of the Visual Studio Manifest Designer—or on your tiles and toasts. By using qualifiers in your images' file names, and optionally dynamically loading them with the help of a [**ResourceContext**](/uwp/api/windows.applicationmodel.resources.core.resourcecontext?branch=live), you can cause the most appropriate image file to be loaded that best matches the user's runtime settings for display scale, theme, high contrast, language, and other contexts.
10+
11+
# Load images and assets tailored for scale, theme, high contrast, and more
12+
13+
Your app can load image resource files (or other asset files) tailored for [display scale factor](/windows/apps/design/layout/screen-sizes-and-breakpoints-for-responsive-design), theme, high contrast, and other runtime contexts. These images can be referenced from imperative code or from XAML markup, for example as the **Source** property of an **Image**. They can also appear in your app package manifest source file (the `Package.appxmanifest` file)—for example, as the value for App Icon on the Visual Assets tab of the Visual Studio Manifest Designer—or on your tiles and toasts. By using qualifiers in your images' file names, and optionally dynamically loading them with the help of a [**ResourceContext**](/uwp/api/windows.applicationmodel.resources.core.resourcecontext), you can cause the most appropriate image file to be loaded that best matches the user's runtime settings for display scale, theme, high contrast, language, and other contexts.
1214

1315
An image resource is contained in an image resource file. You can also think of the image as an asset, and the file that contains it as an asset file; and you can find these kinds of resource files in your project's \Assets folder. For background on how to use qualifiers in the names of your image resource files, see [Tailor your resources for language, scale, and other qualifiers](tailor-resources-lang-scale-contrast.md).
1416

1517
Some common qualifiers for images are [scale](tailor-resources-lang-scale-contrast.md#scale), [theme](tailor-resources-lang-scale-contrast.md#theme), [contrast](tailor-resources-lang-scale-contrast.md#contrast), and [targetsize](tailor-resources-lang-scale-contrast.md#targetsize).
1618

1719
## Qualify an image resource for scale, theme, and contrast
18-
The default value for the `scale` qualifier is `scale-100`. So, these two variants are equivalent (they both provide an image at scale 100, or scale factor 1).
1920

21+
The default value for the `scale` qualifier is `scale-100`. So, these two variants are equivalent (they both provide an image at scale 100, or scale factor 1).
2022

21-
<pre>
23+
```
2224
\Assets\Images\logo.png
2325
\Assets\Images\logo.scale-100.png
24-
</pre>
25-
26-
26+
```
2727

2828
You can use qualifiers in folder names instead of file names. That would be a better strategy if you have several asset files per qualifier. For purposes of illustration, these two variants are equivalent to the two above.
2929

30-
31-
<pre>
30+
```
3231
\Assets\Images\logo.png
3332
\Assets\Images\scale-100\logo.png
34-
</pre>
35-
33+
```
3634

3735
Next is an example of how you can provide variants of an image resource&mdash;named `/Assets/Images/logo.png`&mdash;for different settings of display scale, theme, and high contrast. This example uses folder naming.
3836

39-
40-
<pre>
37+
```
4138
\Assets\Images\contrast-standard\theme-dark
4239
\scale-100\logo.png
4340
\scale-200\logo.png
@@ -47,10 +44,10 @@ Next is an example of how you can provide variants of an image resource&mdash;na
4744
\Assets\Images\contrast-high
4845
\scale-100\logo.png
4946
\scale-200\logo.png
50-
</pre>
51-
47+
```
5248

5349
## Reference an image or other asset from XAML markup and code
50+
5451
The name&mdash;or identifier&mdash;of an image resource is its path and file name with any and all qualifiers removed. If you name folders and/or files as in any of the examples in the previous section, then you have a single image resource and its name (as an absolute path) is `/Assets/Images/logo.png`. Here’s how you use that name in XAML markup.
5552

5653
```xaml
@@ -96,46 +93,42 @@ Absolute paths are a good choice if your image files remain where they are in th
9693
Also see [Tile and toast support for language, scale, and high contrast](/windows/apps/design/shell/tiles-and-notifications/tile-toast-language-scale-contrast).
9794

9895
## Qualify an image resource for targetsize
99-
You can use the `scale` and `targetsize` qualifiers on different variants of the same image resource; but you can't use them both on a single variant of a resource. Also, you need to define at least one variant without a `TargetSize` qualifier. That variant must either define a value for `scale`, or let it default to `scale-100`. So, these two variants of the `/Assets/Square44x44Logo.png` resource are valid.
10096

97+
You can use the `scale` and `targetsize` qualifiers on different variants of the same image resource; but you can't use them both on a single variant of a resource. Also, you need to define at least one variant without a `TargetSize` qualifier. That variant must either define a value for `scale`, or let it default to `scale-100`. So, these two variants of the `/Assets/Square44x44Logo.png` resource are valid.
10198

102-
<pre>
99+
```
103100
\Assets\Square44x44Logo.scale-200.png
104101
\Assets\Square44x44Logo.targetsize-24.png
105-
</pre>
106-
102+
```
107103

108104
And these two variants are valid.
109105

110-
111-
<pre>
106+
```
112107
\Assets\Square44x44Logo.png // defaults to scale-100
113108
\Assets\Square44x44Logo.targetsize-24.png
114-
</pre>
115-
109+
```
116110

117111
But this variant is not valid.
118112

119-
120-
<pre>
113+
```
121114
\Assets\Square44x44Logo.scale-200_targetsize-24.png
122-
</pre>
123-
115+
```
124116

125117
## Refer to an image file from your app package manifest
118+
126119
If you name folders and/or files as in either of the two valid examples in the previous section, then you have a single app icon image resource and its name (as a relative path) is `Assets\Square44x44Logo.png`. In your app package manifest, simply refer to the resource by name. There's no need to use any URI scheme.
127120

128121
![add resource, english](images/app-icon.png)
129122

130-
That's all you need to do, and the OS will perform automatic qualifier matching to find the resource that's most appropriate for the current context. For a list of all items in the app package manifest that you can localize or otherwise qualify in this way, see [Localizable manifest items](/uwp/schemas/appxpackage/uapmanifestschema/localizable-manifest-items-win10?branch=live).
123+
That's all you need to do, and the OS will perform automatic qualifier matching to find the resource that's most appropriate for the current context. For a list of all items in the app package manifest that you can localize or otherwise qualify in this way, see [Localizable manifest items](/uwp/schemas/appxpackage/uapmanifestschema/localizable-manifest-items-win10).
131124

132125
## Qualify an image resource for layoutdirection
133126
See [Mirroring images](/windows/apps/design/globalizing/adjust-layout-and-fonts--and-support-rtl#mirroring-images).
134127

135128
## Load an image for a specific language or other context
136129
For more info about the value proposition of localizing your app, see [Globalization and localization](/windows/apps/design/globalizing/globalizing-portal).
137130

138-
The default [**ResourceContext**](/uwp/api/windows.applicationmodel.resources.core.resourcecontext?branch=live) (obtained from [**ResourceContext.GetForCurrentView**](/uwp/api/windows.applicationmodel.resources.core.resourcecontext.GetForCurrentView)) contains a qualifier value for each qualifier name, representing the default runtime context (in other words, the settings for the current user and machine). Image files are matched&mdash;based on the qualifiers in their names&mdash;against the qualifier values in that runtime context.
131+
The default [**ResourceContext**](/uwp/api/windows.applicationmodel.resources.core.resourcecontext) (obtained from [**ResourceContext.GetForCurrentView**](/uwp/api/windows.applicationmodel.resources.core.resourcecontext.GetForCurrentView)) contains a qualifier value for each qualifier name, representing the default runtime context (in other words, the settings for the current user and machine). Image files are matched&mdash;based on the qualifiers in their names&mdash;against the qualifier values in that runtime context.
139132

140133
But there might be times when you want your app to override the system settings and be explicit about the language, scale, or other qualifier value to use when looking for a matching image to load. For example, you might want to control exactly when and which high contrast images are loaded.
141134

@@ -152,7 +145,7 @@ bitmapImage.SetSourceAsync(imageFileStream);
152145
this.myXAMLImageElement.Source = bitmapImage;
153146
```
154147

155-
For the same effect at a global level, you *can* override the qualifier values in the default **ResourceContext**. But instead we advise you to call [**ResourceContext.SetGlobalQualifierValue**](/uwp/api/windows.applicationmodel.resources.core.resourcecontext.setglobalqualifiervalue?branch=live#Windows_ApplicationModel_Resources_Core_ResourceContext_SetGlobalQualifierValue_System_String_System_String_Windows_ApplicationModel_Resources_Core_ResourceQualifierPersistence_). You set values one time with a call to **SetGlobalQualifierValue** and then those values are in effect on the default **ResourceContext** each time you use it for lookups. By default, the [**ResourceManager**](/uwp/api/windows.applicationmodel.resources.core.resourcemanager?branch=live) class uses the default **ResourceContext**.
148+
To achieve same effect at a global level, you *can* override the qualifier values in the default **ResourceContext**. But instead we advise you to call [**ResourceContext.SetGlobalQualifierValue**](/uwp/api/windows.applicationmodel.resources.core.resourcecontext.setglobalqualifiervalue#Windows_ApplicationModel_Resources_Core_ResourceContext_SetGlobalQualifierValue_System_String_System_String_Windows_ApplicationModel_Resources_Core_ResourceQualifierPersistence_). You set values one time with a call to **SetGlobalQualifierValue** and then those values are in effect on the default **ResourceContext** each time you use it for lookups. By default, the [**ResourceManager**](/uwp/api/windows.applicationmodel.resources.core.resourcemanager) class uses the default **ResourceContext**.
156149

157150
```csharp
158151
Windows.ApplicationModel.Resources.Core.ResourceContext.SetGlobalQualifierValue("Contrast", "high");
@@ -161,9 +154,10 @@ this.myXAMLImageElement.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(n
161154
```
162155

163156
## Updating images in response to qualifier value change events
164-
Your running app can respond to changes in system settings that affect the qualifier values in the default resource context. Any of these system settings invokes the [**MapChanged**](/uwp/api/windows.foundation.collections.iobservablemap-2.mapchanged?branch=live) event on [**ResourceContext.QualifierValues**](/uwp/api/windows.applicationmodel.resources.core.resourcecontext.QualifierValues).
165157

166-
In response to this event, you can reload your images with the help of the default **ResourceContext**, which [**ResourceManager**](/uwp/api/windows.applicationmodel.resources.core.resourcemanager?branch=live) uses by default.
158+
Your running app can respond to changes in system settings that affect the qualifier values in the default resource context. Any of these system settings invokes the [**MapChanged**](/uwp/api/windows.foundation.collections.iobservablemap-2.mapchanged) event on [**ResourceContext.QualifierValues**](/uwp/api/windows.applicationmodel.resources.core.resourcecontext.QualifierValues).
159+
160+
In response to this event, you can reload your images with the help of the default **ResourceContext**, which [**ResourceManager**](/uwp/api/windows.applicationmodel.resources.core.resourcemanager) uses by default.
167161

168162
```csharp
169163
public MainPage()
@@ -198,11 +192,13 @@ private void RefreshUIImages()
198192
```
199193

200194
## Important APIs
195+
201196
* [ResourceContext](/uwp/api/windows.applicationmodel.resources.core.resourcecontext?branch=live)
202197
* [ResourceContext.SetGlobalQualifierValue](/uwp/api/windows.applicationmodel.resources.core.resourcecontext.setglobalqualifiervalue?branch=live#Windows_ApplicationModel_Resources_Core_ResourceContext_SetGlobalQualifierValue_System_String_System_String_Windows_ApplicationModel_Resources_Core_ResourceQualifierPersistence_)
203198
* [MapChanged](/uwp/api/windows.foundation.collections.iobservablemap-2.mapchanged?branch=live)
204199

205200
## Related topics
201+
206202
* [Tailor your resources for language, scale, and other qualifiers](tailor-resources-lang-scale-contrast.md)
207203
* [Localize strings in your UI and app package manifest](localize-strings-ui-manifest.md)
208204
* [Store and retrieve settings and other app data](/windows/apps/design/app-settings/store-and-retrieve-app-data)

0 commit comments

Comments
 (0)