Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] CaptureAsync produces incorrect image on Android #2768

Open
1 task done
Pairoba opened this issue Feb 27, 2024 · 1 comment
Open
1 task done

[BUG] CaptureAsync produces incorrect image on Android #2768

Pairoba opened this issue Feb 27, 2024 · 1 comment
Labels

Comments

@Pairoba
Copy link

Pairoba commented Feb 27, 2024

Description

I'm creating a custom control using SKCanvasView. Putting this into a view and then using CaptureAsync on the view is producing a wrong image on android. Works on Windows.

The top view was captured and the bottom is the captured image:

image

Code

GitHub repository: https://github.com/Pairoba/CaptureAsyncSkiaSharp

CustomView.cs

internal class CustomView : SKCanvasView
{
    protected override void OnPaintSurface(SKPaintSurfaceEventArgs e)
    {
        base.OnPaintSurface(e);
        var canvas = e.Surface.Canvas;
        var info = e.Info;

        using var paint = new SKPaint();
        paint.Color = SKColors.Black;
        paint.StrokeWidth = 10;

        canvas.DrawLine(0, 0, info.Width, info.Height, paint);
        canvas.DrawLine(info.Width, 0, 0, info.Height, paint);
    }
}

MainPage.xaml.cs

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    private async void CaptureAsync(object sender, EventArgs e)
    {
        var snap = await CaptureTarget.CaptureAsync();
        if (snap != null)
        {
            var s = await snap.OpenReadAsync();
            CaptureImageView.Source = ImageSource.FromStream(() => s);
        }
    }
}

MainPage.xaml

<VerticalStackLayout>
    <Border x:Name="CaptureTarget"
            HeightRequest="250"
            WidthRequest="250"
            StrokeThickness="10"
            Stroke="Blue">
        <AbsoluteLayout BackgroundColor="Green">
            <local:CustomView AbsoluteLayout.LayoutBounds="40, 40, 100, 100"
                              AbsoluteLayout.LayoutFlags="None" />
        </AbsoluteLayout>
    </Border>
    <Button Text="Capture Async"
            Clicked="CaptureAsync"/>
    <Image x:Name="CaptureImageView"
           HeightRequest="250"
           WidthRequest="250"/>
</VerticalStackLayout>

Expected Behavior

Correct generated image

Actual Behavior

Incorrect generated image

Version of SkiaSharp

2.88.3 (Current)

Last Known Good Version of SkiaSharp

Other (Please indicate in the description)

IDE / Editor

Visual Studio (Windows)

Platform / Operating System

Android

Platform / Operating System Version

  • Android 13
  • Android 14

Devices

  • Pixel 5 Emulator

Relevant Screenshots

No response

Relevant Log Output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@Pairoba
Copy link
Author

Pairoba commented Feb 28, 2024

It seems like, that the canvas on the snapshot is scaled to the snapshot target's size. So I was able to create a Workaround by scaling the CustomView during the snapshot creation.

private async void CaptureAsync(object sender, EventArgs e)
{
    CustomViewInstance.AnchorX = 0;
    CustomViewInstance.AnchorY = 0;
    CustomViewInstance.ScaleX = CustomViewInstance.Width / CaptureTarget.Width;
    CustomViewInstance.ScaleY = CustomViewInstance.Height / CaptureTarget.Height;

    var snap = await CaptureTarget.CaptureAsync();
    if (snap != null)
    {
        var s = await snap.OpenReadAsync();
        CaptureImageView.Source = ImageSource.FromStream(() => s);
    }

    CustomViewInstance.ScaleX = 1;
    CustomViewInstance.ScaleY = 1;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Ready For Work
Development

No branches or pull requests

1 participant