title: How to call PaintSurface twice in skiasharp xamarin forms
body: <p>I am doing a project wherein I can change the filter of the image. I am using skiasharp to change the filter of the image. It is like that of CamScanner Application. But when I change the filter to grayscale first and then Lighten and then Sepia and then again back to grayscale I hit save I get Sepia's Image. I understand that the last data being generated is that of sepia's hence it is saving that data. But if I want to change more than 3 times it is not working. Please help me out. Here is my coding.</p>
  
  <pre><code>  &lt;ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                   xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                   xmlns:skia="clr-namespace:SkiaSharp.Views.Forms;assembly=SkiaSharp.Views.Forms"
                   BackgroundColor="Black"
                   x:Class="JC.EditPage"&gt;
          &lt;ContentPage.Content&gt;
  
              &lt;StackLayout Padding="10,10,10,10" Orientation="Vertical"&gt;
  
  
                  &lt;Image x:Name="imageView" HeightRequest="450"
                         HorizontalOptions="FillAndExpand"
                         VerticalOptions="CenterAndExpand" IsVisible="True"/&gt;
  
                  &lt;StackLayout x:Name="canvasStackView" IsVisible="False"&gt;
                      &lt;skia:SKCanvasView HeightRequest="450" PaintSurface="OnCanvasViewPaintSurface" VerticalOptions="CenterAndExpand" HorizontalOptions="FillAndExpand"/&gt;
                  &lt;/StackLayout&gt;
  
                  &lt;StackLayout x:Name="canvasLightenStackView" IsVisible="False"&gt;
                      &lt;skia:SKCanvasView HeightRequest="450" PaintSurface="OnCanvasViewPaintSurfaceLighten" VerticalOptions="CenterAndExpand" HorizontalOptions="FillAndExpand"/&gt;
                  &lt;/StackLayout&gt;
  
                  &lt;StackLayout x:Name="canvasSepiaStackView" IsVisible="False"&gt;
                      &lt;skia:SKCanvasView HeightRequest="450" PaintSurface="OnCanvasViewPaintSurfaceSepia" VerticalOptions="CenterAndExpand" HorizontalOptions="FillAndExpand"/&gt;
                  &lt;/StackLayout&gt;
  
        &lt;ScrollView Orientation="Horizontal" HorizontalScrollBarVisibility="Never"&gt;
                      &lt;StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" VerticalOptions="EndAndExpand" x:Name="filterStack" IsVisible="True"&gt;
                          &lt;StackLayout WidthRequest="100" HeightRequest="70" BackgroundColor="White"&gt;
                              &lt;Label Text="Original" FontFamily="Bold" Font="14" TextColor="Black" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"/&gt;
                              &lt;StackLayout.GestureRecognizers&gt;
                                  &lt;TapGestureRecognizer Tapped="Original_Tapped"&gt;
                                  &lt;/TapGestureRecognizer&gt;
                              &lt;/StackLayout.GestureRecognizers&gt;
                          &lt;/StackLayout&gt;
  
                          &lt;StackLayout WidthRequest="100" HeightRequest="70" BackgroundColor="White" &gt;
                              &lt;Label Text="Grayscale" FontFamily="Bold" Font="14" TextColor="Black" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"/&gt;
                              &lt;StackLayout.GestureRecognizers&gt;
                                  &lt;TapGestureRecognizer Tapped="Grayscale_Tapped"&gt;
                                  &lt;/TapGestureRecognizer&gt;
                              &lt;/StackLayout.GestureRecognizers&gt;
                          &lt;/StackLayout&gt;
  
                          &lt;StackLayout WidthRequest="100" HeightRequest="70" BackgroundColor="White"&gt;
                              &lt;Label Text="Lighten" FontFamily="Bold" Font="14" TextColor="Black" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"/&gt;
                              &lt;StackLayout.GestureRecognizers&gt;
                                  &lt;TapGestureRecognizer Tapped="Lighten_Tapped"&gt;
                                  &lt;/TapGestureRecognizer&gt;
                              &lt;/StackLayout.GestureRecognizers&gt;
                          &lt;/StackLayout&gt;
  
                          &lt;StackLayout WidthRequest="100" HeightRequest="70" BackgroundColor="White" &gt;
                              &lt;Label Text="Sepia" FontFamily="Bold" Font="14" TextColor="Black" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"/&gt;
                              &lt;StackLayout.GestureRecognizers&gt;
                                  &lt;TapGestureRecognizer Tapped="Speia_Tapped"&gt;
                                  &lt;/TapGestureRecognizer&gt;
                              &lt;/StackLayout.GestureRecognizers&gt;
                          &lt;/StackLayout&gt;
                      &lt;/StackLayout&gt;
                  &lt;/ScrollView&gt;
  
                  &lt;StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" VerticalOptions="EndAndExpand" BackgroundColor="White"&gt;
                      &lt;Image Source="goback.png" HorizontalOptions="StartAndExpand"&gt;
                          &lt;Image.GestureRecognizers&gt;
                              &lt;TapGestureRecognizer Tapped="goback_Tapped"/&gt;
                          &lt;/Image.GestureRecognizers&gt;
                      &lt;/Image&gt;
  
                      &lt;Image Source="tick.png" HorizontalOptions="EndAndExpand"&gt;
                          &lt;Image.GestureRecognizers&gt;
                              &lt;TapGestureRecognizer Tapped="Save_Tapped"/&gt;
                          &lt;/Image.GestureRecognizers&gt;
                      &lt;/Image&gt;
                  &lt;/StackLayout&gt;
              &lt;/StackLayout&gt;
          &lt;/ContentPage.Content&gt;
      &lt;/ContentPage&gt;
  </code></pre>
  
  <p>and here is my XAML.CS file for that - </p>
  
  <pre><code>  private async void Grayscale_Tapped(object sender, EventArgs e)
      {
          DependencyService.Get&lt;IProgressInterface&gt;().ShowLoader("Please wait...");
  
  
  
          adjust = false;
          canvasEditStackView.IsVisible = false;
          canvasSepiaStackView.IsVisible = false;
          canvasLightenStackView.IsVisible = false;
          imageView.IsVisible = false;
          canvasStackView.IsVisible = true;
          filterStack.IsVisible = true;
          original = false;
  
          byte[] tempArray = await StorageHelper.LoadImage(image, path);
  
         canvasView = new SKCanvasView();
         canvasView.PaintSurface += OnCanvasViewPaintSurface;
  
          using (Stream stream = new MemoryStream(tempArray))
          {
              if (stream != null)
              {
                  libraryBitmap = SKBitmap.Decode(stream);
                  canvasView.InvalidateSurface();
              }
          }
          DependencyService.Get&lt;IProgressInterface&gt;().DismissLoader();
      }
  
      void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
      {
          Console.WriteLine("Hits");
          DependencyService.Get&lt;IProgressInterface&gt;().ShowLoader("Please wait...");
  
          SKImageInfo info = args.Info;
          SKSurface surface = args.Surface;
          SKCanvas canvas = surface.Canvas;
  
          canvas.Clear();
  
          using (SKPaint paint = new SKPaint())
          {
  
              paint.ColorFilter =
                  SKColorFilter.CreateColorMatrix(new float[]
                  {
                  0.21f, 0.72f, 0.07f, 0, 0,
                  0.21f, 0.72f, 0.07f, 0, 0,
                  0.21f, 0.72f, 0.07f, 0, 0,
                  0,     0,     0,     1, 0
                  });
  
              canvas.DrawBitmap(libraryBitmap, info.Rect, BitmapStretch.Uniform, paint: paint);
              DependencyService.Get&lt;IProgressInterface&gt;().DismissLoader();
          }
          var snap = surface.Snapshot();
          SKData data = snap.Encode();
          saveData = data;
  
      }
  
      private async void Lighten_Tapped(object sender, EventArgs e)
      {
          DependencyService.Get&lt;IProgressInterface&gt;().ShowLoader("Please wait...");
  
  
          adjust = false;
          imageView.IsVisible = false;
          canvasEditStackView.IsVisible = false;
          canvasStackView.IsVisible = false;
          canvasSepiaStackView.IsVisible = false;
          canvasLightenStackView.IsVisible = true;
          filterStack.IsVisible = true;
          original = false;
  
          byte[] tempArray = await StorageHelper.LoadImage(image, path);
  
          canvasView = new SKCanvasView();
          canvasView.PaintSurface += OnCanvasViewPaintSurfaceLighten;
  
          using (Stream stream = new MemoryStream(tempArray))
          {
              if (stream != null)
              {
                  libraryBitmap = SKBitmap.Decode(stream);
                  canvasView.InvalidateSurface();
              }
          }
          DependencyService.Get&lt;IProgressInterface&gt;().DismissLoader();
  
      }
  
      void OnCanvasViewPaintSurfaceLighten(object sender, SKPaintSurfaceEventArgs args)
      {
          DependencyService.Get&lt;IProgressInterface&gt;().ShowLoader("Please wait...");
          SKImageInfo info = args.Info;
          SKSurface surface = args.Surface;
          SKCanvas canvas = surface.Canvas;
  
          canvas.Clear();
  
          using (SKPaint paint = new SKPaint())
          {
              paint.ColorFilter =
                  SKColorFilter.CreateColorMatrix(new float[]
                  {
                  0.75f, 0.25f, 0.25f, 0, 0,
                  0.25f, 0.75f, 0.25f, 0, 0,
                  0.25f, 0.25f, 0.75f, 0, 0,
                  0, 0, 0, 1, 0
                  });
              canvas.DrawBitmap(libraryBitmap, info.Rect, BitmapStretch.Uniform, paint: paint);
              DependencyService.Get&lt;IProgressInterface&gt;().DismissLoader();
          }
          var snap = surface.Snapshot();
          SKData data = snap.Encode();
          saveData = data;
  
      }
  
      public async void Speia_Tapped(object sender, EventArgs e)
      {
  
          DependencyService.Get&lt;IProgressInterface&gt;().ShowLoader("Please wait...");
  
  
  
          adjust = false;
          imageView.IsVisible = false;
          canvasEditStackView.IsVisible = false;
          canvasStackView.IsVisible = false;
          canvasLightenStackView.IsVisible = false;
          canvasSepiaStackView.IsVisible = true;
          filterStack.IsVisible = true;
          original = false;
  
          byte[] tempArray = await StorageHelper.LoadImage(image, path);
  
          canvasView = new SKCanvasView();
          canvasView.PaintSurface += OnCanvasViewPaintSurfaceSepia;
  
          using (Stream stream = new MemoryStream(tempArray))
          {
              if (stream != null)
              {
                  libraryBitmap = SKBitmap.Decode(stream);
                  canvasView.InvalidateSurface();
  
              }
          }
          DependencyService.Get&lt;IProgressInterface&gt;().DismissLoader();
      }
  
      void OnCanvasViewPaintSurfaceSepia(object sender, SKPaintSurfaceEventArgs args)
      {
          DependencyService.Get&lt;IProgressInterface&gt;().ShowLoader("Please wait...");
          SKImageInfo info = args.Info;
          SKSurface surface = args.Surface;
          SKCanvas canvas = surface.Canvas;
  
          canvas.Clear();
          using (SKPaint paint = new SKPaint())
          {
              paint.ColorFilter =
                  SKColorFilter.CreateColorMatrix(new float[]
                  {
                    1, 0,   0, 0, 0,
                    0, 1,   0, 0, 0,
                    0, 0, 0.8f, 0, 0,
                    0, 0,   0, 1, 0
                  });
              canvas.DrawBitmap(libraryBitmap, info.Rect, BitmapStretch.Uniform, paint: paint);
              DependencyService.Get&lt;IProgressInterface&gt;().DismissLoader();
          }
          var snap = surface.Snapshot();
          SKData data = snap.Encode();
          saveData = data;
      }
  </code></pre>
  
  <p>and this is my save command.</p>
  
  <pre><code> if (original == true)
              {
                  var editPref = DependencyService.Get&lt;IUserPreferences&gt;();
                  editPref.SaveString("edit", "false");
  
                  await Navigation.PushModalAsync(new desiredLocationStoragePage(path));
              }
              else
              {
                  var editPref = DependencyService.Get&lt;IUserPreferences&gt;();
                  editPref.SaveString("edit", "true");
                  string saveName = fileName;
                  using (var stream = File.OpenWrite(saveName))
                  {
                      saveData.SaveTo(stream);
                  }
                  await Navigation.PushModalAsync(new desiredLocationStoragePage(fileName));
  
              }
  
  
  
     please help me out guys I am quite stuck after this phase
  </code></pre>