SkiaSharp random noise in empty images

Viewed 695

I'm slicing some images with SkiaSharp, some pictures don't cover the whole bitmap, and looks like there is random noise on some of the pictures.

The simplest was to reproduce is just create new empty bitmaps & save them in a loop. Something like this:

// imgSize = 224
// savePath is a new file path every time
var bm = new SKBitmap(imgSize, imgSize);
var subSaveStream = File.OpenWrite(savePath);
var subImage = SKImage.FromBitmap(bm).Encode(SKEncodedImageFormat.Png, 100);
subImage.SaveTo(subSaveStream);

First thing to note is the saved file sizes go from 305 byte to up to 50k! Some have barely visible artifacts, some have crazy noise patterns like this:

noisy image

My question:

I guess I could just "erase it black" like this ex.Erase(SKColors.Black), and done. But is this expected?

Environment:

I can reproduce it on my machine (Win10 laptop) and a Debian server, so let's assume it's not my machine.

SkiaSharp 1.68 + .NET Core SDK 2.2.300

Plus SkiaSharp.NativeAssets.Linux 1.68 on Debian 9

1 Answers

When a bitmap/image/surface is created, it does not clear the memory. What you are seeing is the raw memory data.

The choice to not clear the image for you is for performance (no use clearing all the pixels to a color, when you are going to do it on the very next command) and what color should it pick?

Of course, you could argue that picking transparent would fix this "error", but this was never really a major issue.

Related