Aller au contenu

Images et formes

Images bitmap, icônes vectorielles et formes géométriques.

Image

<Image Source="/Assets/logo.png" Width="120" />
Aperçu · image 2:1 dans une zone carrée
Uniform
UniformToFill
Fill
None
Stretch Effet
Uniform tient dans la zone, proportions gardées (défaut)
UniformToFill remplit, rogne le surplus
Fill remplit en déformant
None taille d'origine

Avatar rond

Aperçu · Border rond + ClipToBounds + UniformToFill
<Border Width="64" Height="64" CornerRadius="32" ClipToBounds="True">
  <Image Source="/Assets/avatar.jpg" Stretch="UniformToFill" />
</Border>

Qualité de mise à l'échelle

Aperçu · un sprite 8×8 agrandi
par défaut : lissé, flou
BitmapInterpolationMode="None" : pixels nets
<!-- Pixel art net -->
<Image Source="/Assets/sprite.png" RenderOptions.BitmapInterpolationMode="None" Width="128" />

Image depuis le code

using Avalonia.Media.Imaging;
using Avalonia.Platform;

// Asset embarqué
Logo.Source = new Bitmap(AssetLoader.Open(new Uri("avares://MonApp/Assets/logo.png")));

// Fichier sur disque
Photo.Source = new Bitmap(@"C:\Images\photo.jpg");

// Redimensionnée au chargement (économise la mémoire)
await using var flux = File.OpenRead(chemin);
Miniature.Source = Bitmap.DecodeToWidth(flux, 200);

Image depuis Internet

using var http = new HttpClient();
var octets = await http.GetByteArrayAsync("https://exemple.fr/image.png");
Photo.Source = new Bitmap(new MemoryStream(octets));

PathIcon

Icône vectorielle qui prend la couleur Foreground. Nette à toutes les tailles.

Aperçu · même Data, tailles et couleurs différentes
<PathIcon Data="M12 2l3 7h7l-5.5 4 2 7L12 16l-6.5 4 2-7L2 9h7z"
          Width="24" Height="24" Foreground="#F59E0B" />

Bibliothèque d'icônes en ressources

App.axaml
<Application.Resources>
  <StreamGeometry x:Key="IconeAccueil">M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z</StreamGeometry>
  <StreamGeometry x:Key="IconeParametres">M19.4 13a7.6 7.6 0 0 0 0-2l2.1-1.6-2-3.5-2.5 1a7.4 7.4 0 0 0-1.7-1L15 3h-4l-.3 2.9a7.4 7.4 0 0 0-1.7 1l-2.5-1-2 3.5L6.6 11a7.6 7.6 0 0 0 0 2l-2.1 1.6 2 3.5 2.5-1a7.4 7.4 0 0 0 1.7 1L11 21h4l.3-2.9a7.4 7.4 0 0 0 1.7-1l2.5 1 2-3.5zM13 15.5a3.5 3.5 0 1 1 0-7 3.5 3.5 0 0 1 0 7z</StreamGeometry>
</Application.Resources>
<PathIcon Data="{StaticResource IconeAccueil}" />

Tip

Les tracés SVG (attribut d) des bibliothèques comme Material Design Icons ou Fluent UI Icons se collent tels quels dans Data.

Formes

Aperçu · Rectangle, Ellipse, Line, Polygon, Polyline
<StackPanel Orientation="Horizontal" Spacing="16">
  <Rectangle Width="60" Height="40" Fill="#A78BFA" RadiusX="6" RadiusY="6" />
  <Ellipse Width="40" Height="40" Fill="#6D28D9" Stroke="#4C1D95" StrokeThickness="2" />
  <Line StartPoint="0,0" EndPoint="60,40" Stroke="Gray" StrokeThickness="2" />
  <Polygon Points="0,40 30,0 60,40" Fill="#F59E0B" />
  <Polyline Points="0,30 15,10 30,25 45,5 60,20" Stroke="#10B981" StrokeThickness="2" />
</StackPanel>

Path

Le mini-langage Data : M = aller à, L = ligne, C = courbe de Bézier, Z = fermer.

Aperçu · M 0,50 C 30,0 70,100 100,50
violet : début et fin · rouge : points de contrôle qui « tirent » la courbe
<Path Data="M 0,50 C 30,0 70,100 100,50" Stroke="#6D28D9" StrokeThickness="3" />

Pointillés et extrémités

<Line StartPoint="0,0" EndPoint="200,0" Stroke="Gray" StrokeThickness="2"
      StrokeDashArray="4,2" StrokeLineCap="Round" />

Dégradés

Aperçu · Linear (0%,0% → 100%,100%) et Radial

StartPoint / EndPoint donnent la direction : 0%,0% = coin haut-gauche, 100%,100% = coin bas-droit.

<Border Height="80" CornerRadius="8">
  <Border.Background>
    <LinearGradientBrush StartPoint="0%,0%" EndPoint="100%,100%">
      <GradientStop Offset="0" Color="#8B5CF6" />
      <GradientStop Offset="1" Color="#EC4899" />
    </LinearGradientBrush>
  </Border.Background>
</Border>

<Ellipse Width="100" Height="100">
  <Ellipse.Fill>
    <RadialGradientBrush RadiusX="50%" RadiusY="50%">
      <GradientStop Offset="0" Color="White" />
      <GradientStop Offset="1" Color="#6D28D9" />
    </RadialGradientBrush>
  </Ellipse.Fill>
</Ellipse>

v12

RadialGradientBrush.Radius est remplacé par RadiusX et RadiusY.

Image en fond

<Border Height="160" CornerRadius="12">
  <Border.Background>
    <ImageBrush Source="/Assets/fond.jpg" Stretch="UniformToFill" />
  </Border.Background>
</Border>

Effets

Aperçu · DropShadowEffect et BlurEffect
<Border Background="#6D28D9" Width="80" Height="80" CornerRadius="8">
  <Border.Effect>
    <DropShadowEffect BlurRadius="16" OffsetX="0" OffsetY="4" Opacity="0.4" />
  </Border.Effect>
</Border>

<Image Source="/Assets/photo.jpg">
  <Image.Effect>
    <BlurEffect Radius="8" />
  </Image.Effect>
</Image>