Forum Libraries, Components, and Plugins


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

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()
screenShareImage.material.mainTexture = tex;

Sign In or Register to comment.