For .NET MAUI, the approach to handling images is different and more streamlined than Xamarin.Forms. Here's what you need to know:
SVG vs. Bitmap Images in MAUI
Yes, you can (and should) primarily use SVG images in MAUI because:
MAUI has built-in SVG support (via
Microsoft.Maui.Graphics
)SVGs scale perfectly to any resolution
Single file maintenance (no need for multiple density versions)
Smaller app size (one file instead of multiple bitmaps)
Recommended Approach
Use SVG as your primary format for icons, logos, and simple graphics
Use PNG only when necessary (for complex images, photos, or when SVG rendering isn't perfect)
For PNGs, provide 1x-3x versions (MAUI handles scaling better than Xamarin)
Image Sizes for MAUI
Unlike Xamarin.Forms, you don't need to provide all those density-specific versions. MAUI recommends:
For SVG:
Single SVG file in your
Resources/Images
folderMAUI will automatically scale it appropriately
For PNG (when needed):
myimage.png (1x - baseline) myimage@2x.png (2x) myimage@3x.png (3x)
Folder Structure
Place your images in:
Resources/ Images/ logo.svg icon.png icon@2x.png icon@3x.png
Usage in XAML
<Image Source="logo.svg" WidthRequest="200" HeightRequest="100"/> <!-- or --> <Image Source="icon.png"/>
When to Use Multiple PNG Versions
Consider providing multiple PNG versions when:
The image contains photographic content
The SVG renders poorly on some devices
You need pixel-perfect rendering at specific sizes
Performance Considerations
SVG rendering has a small performance cost during loading (but is cached)
For frequently used images, consider:
Using PNG for better performance
Pre-rendering SVG to bitmap for complex graphics
Migration from Xamarin.Forms
If migrating from Xamarin.Forms:
Start by replacing all density-specific images with SVGs
Only keep multiple PNG versions where SVG doesn't work well
Remove all the old density folders (
drawable-hdpi
, etc.)
iOS/Android Specific Notes
MAUI handles the platform-specific details for you
No need for platform-specific image folders
The same Resources/Images folder works for all platforms
This approach significantly simplifies your image management while maintaining high quality across all devices.
No comments:
Post a Comment