Xamarin.Forms - Tint Image Color Using Effects

Introduction

 
Xamarin.Forms code runs on multiple platforms, each of which has its own filesystem. This means that reading and writing files are most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.
 

Tint Image

 
TintImage is used for changing image color at runtime with the help of effects, you can achieve this with custom render also. Tint image color is mainly used to reduce the usage of the images.
 
Prerequisites
  • Visual Studio 2017 or later (Windows or Mac)

Setting up a Xamarin.Forms Project

 
Start by creating a new Xamarin.Forms project. You wíll learn more by going through the steps yourself.
 
Create a new or existing Xamarin forms (.Net standard) Project with Android and iOS Platform.
 
 

Create TintImage

 
Now, Create a TintImage class inherited from RoutingEffect for changing image color.
 
TintImage.cs
  1. public class TintImage: RoutingEffect  
  2.     {  
  3.         public Color TintColor { getprivate set; }  
  4.         public TintImage(Color color) : base($"MyCompany.{nameof(TintImage)}")  
  5.         {  
  6.             TintColor = color;  
  7.         }  
  8.           
  9.     }   

Android Implementation

 
Here, implement platform effects for TintImage at runtime.
 
TintImageImpl.cs
  1. [assembly: ExportEffect(typeof(TintImageImpl), nameof(XamarinStudy.Common.Controls.TintImage))]  
  2. namespace XamarinStudy.Droid.Effects  
  3. {  
  4.     public class TintImageImpl: PlatformEffect  
  5.     {  
  6.         protected override void OnAttached()  
  7.         {  
  8.             try  
  9.             {  
  10.                 var effect = (XamarinStudy.Common.Controls.TintImage)Element.Effects.FirstOrDefault(e => e is XamarinStudy.Common.Controls.TintImage);  
  11.   
  12.                 if (effect == null || !(Control is ImageView image))  
  13.                     return;  
  14.   
  15.                 var filter = new PorterDuffColorFilter(effect.TintColor.ToAndroid(), PorterDuff.Mode.SrcIn);  
  16.                 image.SetColorFilter(filter);  
  17.             }  
  18.             catch (Exception ex)  
  19.             {  
  20.                   
  21.             }  
  22.         }  
  23.   
  24.         protected override void OnDetached() { }  
  25.     }  
  26. }  

iOS Implementation

 
Here, implement platform effects for TintImage at runtime.
 
TintImageImpl.cs
  1. [assembly: ExportEffect(typeof(XamarinStudy.iOS.Effects.TintImageImpl), nameof(TintImage))]  
  2. namespace XamarinStudy.iOS.Effects  
  3. {  
  4.     public class TintImageImpl: PlatformEffect  
  5.     {  
  6.         protected override void OnAttached()  
  7.         {  
  8.             try  
  9.             {  
  10.                 var effect = (XamarinStudy.Common.Controls.TintImage)Element.Effects.FirstOrDefault(e => e is XamarinStudy.Common.Controls.TintImage);  
  11.   
  12.                 if (effect == null || !(Control is UIImageView image))  
  13.                     return;  
  14.   
  15.                 image.Image = image.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);  
  16.                 image.TintColor = effect.TintColor.ToUIColor();  
  17.             }  
  18.             catch (Exception ex)  
  19.             {  
  20.             }  
  21.         }  
  22.   
  23.         protected override void OnDetached() { }  
  24.     }  
  25. }  

Consuming the TintImage

 
Now, use the TintImage effect in your XAML code.
 
MainPage.xaml
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:d="http://xamarin.com/schemas/2014/forms/design"  
  5.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  6.              mc:Ignorable="d"  
  7.              xmlns:behaviors="clr-namespace:XamarinStudy.Common.Behaviors"  
  8.              xmlns:converters="clr-namespace:XamarinStudy.Common.Converters"  
  9.              xmlns:Effects="clr-namespace:XamarinStudy.Common.Controls"  
  10.              x:Class="XamarinStudy.MainPage" Title="Effects">  
  11.      
  12.    <StackLayout Margin="10,30,10,0" Spacing="20">  
  13.         <Image Source="banner.png"/>  
  14.         <Image Source="favicon.png" Effects:TintImageEffect.TintColor="Green">  
  15.          </Image>  
  16.           
  17.    </StackLayout>  
  18.       
  19. </ContentPage>  
Click the "Play" button to try it out.
 
Actually the image color is gray.
 
 
Wow, it's working.
 
I hope you have understood how to change image color using Tint Image Effects in Xamarin.Forms.
 
Thanks for reading. Please share your comments and feedback. Happy Coding :)


Similar Articles