A Problem with saving image

Yusuf_AYusuf_A Member ✭✭
edited June 1 in General

Hello all
I want to save image to file , but when I save I get the old image
Thank you in advance

 namespace App3
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Page2 : ContentPage
    {
        SKBitmap bitmap;
        SKBitmap newBitmap;
        private static readonly Random getrandom = new Random();

        public Page2(SKBitmap bitmap)
        {
            this.bitmap = bitmap;
            InitializeComponent();
        }

        private void CanvasView_PaintSurface(object sender, SkiaSharp.Views.Forms.SKPaintSurfaceEventArgs e)
        {
            SKSurface surface = e.Surface;
            SKCanvas canvas = surface.Canvas;
            SKImageInfo info = e.Info;

            SKPaint circlePaint = new SKPaint();
            circlePaint.Color = SKColors.Navy;
            circlePaint.Style = SKPaintStyle.Fill;

            canvas.Clear();

            using (SKCanvas newCanvas = new SKCanvas(bitmap))
            {
                canvas.DrawBitmap(bitmap, 0, 0);
                for (int y = 0; y < 4; y++)
                {
                    for (int x = 0; x < 4; x++)
                    {
                        canvas.DrawCircle(x * 100, y * 100, 50, circlePaint);
                    }
                }
            }
            newBitmap = bitmap;
        }


        private async void Button_Clicked(object sender, EventArgs e)
        {
            using (SKImage image = SKImage.FromBitmap(newBitmap))
            {
                SKData snap = image.Encode();
                string filename = ("Test.png");
                IPhotoLibrary photoLibrary = DependencyService.Get<IPhotoLibrary>();
                bool result = await photoLibrary.SavePhotoAsync(snap.ToArray(), "Test", filename);
            }
        }
    }
}

Best Answer

  • Yusuf_AYusuf_A ✭✭
    Accepted Answer

    I fix the code by moving all drawin function to new function
    and call it from main function

      private void Drawing()
            {
    
                SKPaint paint = new SKPaint();
    
    
               SKBitmap   newBitmap = new SKBitmap(bitmap.Width, bitmap.Height);
               using (SKCanvas canvas = new SKCanvas(newBitmap))
                {
    
                    canvas.Clear();
                    ...
                }
             bitmap = newBitmap;
             canvas.InvalidateSurface();
            }
    

Answers

  • BrianConradBrianConrad USMember ✭✭✭

    I mentioned this a couple weeks back and I think it is a bug and may file a bug report. Definitely we should be getting the current image data not the first one.

  • Yusuf_AYusuf_A Member ✭✭
    Accepted Answer

    I fix the code by moving all drawin function to new function
    and call it from main function

      private void Drawing()
            {
    
                SKPaint paint = new SKPaint();
    
    
               SKBitmap   newBitmap = new SKBitmap(bitmap.Width, bitmap.Height);
               using (SKCanvas canvas = new SKCanvas(newBitmap))
                {
    
                    canvas.Clear();
                    ...
                }
             bitmap = newBitmap;
             canvas.InvalidateSurface();
            }
    
Sign In or Register to comment.