SkiaSharp capturing desktop screenshot

Hi everyone,
I'm new here and happen to come by SkiaSharp while researching. I was hoping someone might know what I'm missing exactly. I am trying to capture a desktop screenshot in one thread, then render it on a gameObject in the main thread. I'm doing this constantly so making a screenshare video. I have made this work already using System.Drawing in editor, but of course Unity doesn't play nice with System.Drawing once built thus why I want to use SkiaSharp.

The code below seemed to have read something but I always end up getting a black texture. Do I need to do something on setting up the SKCanvas before creating a surface or snapshot? Any help towards this would be greatly appreciated. Thank you for taking your time, have a great day.

// This part happens on a different thread.
SKSurface surface = SkiaSharp.SKSurface.Create((int)rect.width, (int)rect.height, SKColorType.Bgra8888, SKAlphaType.Premul);
SKBitmap skbmp = SKBitmap.FromImage(surface.Snapshot());
SKImage image = SKImage.FromBitmap(skbmp);
SKData encoded = image.Encode(SKEncodedImageFormat.Jpeg, 80);
screenBytes = encoded.ToArray();

//Back on the main thread I do the following
void Start()
{
...
tex = new Texture2D((int)instance.screenRect.width, (int)instance.screenRect.height, TextureFormat.BGRA32, false);
}

void Update()
{
...
tex.LoadImage(screenBytes);
screenShareImage.material.mainTexture = tex;
}

Tagged:
Sign In or Register to comment.