扩展SKImage的最快方法(SkiaSharp)

时间:2018-01-24 12:38:50

标签: c# .net image-processing skiasharp

我正在寻找调整SKImage大小的最快方法。不幸的是我发现了一些例子。

我问这个是因为如果我并行执行这些功能(在我的情况下大约有60个线程),每个单个缩放功能的执行时间增加到20倍。

我尝试使用以下方法,性能看起来非常相似,有什么更好的吗?

方法1:

SKImage src = (...);
SKImageInfo info = new SKImageInfo(width, height, SKColorType.Bgra8888);
SKImage output = SKImage.Create(info);
src.ScalePixels(output.PeekPixels(), SKFilterQuality.None);

方法2:

SKImage src = (...);
SKImage output;
float resizeFactorX = (float)width / (float)Src.Width;
float resizeFactorY = (float)height / (float)Src.Height;

using (SKSurface surface = SKSurface.Create((int)(Src.Width * 
       resizeFactorX), (int)(Src.Height * resizeFactorY),
       SKColorType.Bgra8888, SKAlphaType.Opaque))
{
  surface.Canvas.SetMatrix(SKMatrix.MakeScale(resizeFactorX, resizeFactorY));
  surface.Canvas.DrawImage(Src, 0, 0);
  surface.Canvas.Flush();
  output = surface.Snapshot();
}

1 个答案:

答案 0 :(得分:0)

这是我使用的代码。另外一个想法是确保将您的SKImage对象包裹在using中,以确保它们能够快速处理掉。我不确定这是否会导致每次迭代减速。

using (var surface = SKSurface.Create(resizedWidth, resizedHeight,
    SKImageInfo.PlatformColorType, SKAlphaType.Premul))
using (var paint = new SKPaint())
{
    // high quality with antialiasing
    paint.IsAntialias = true;
    paint.FilterQuality = SKFilterQuality.High;

    // draw the bitmap to fill the surface
    surface.Canvas.DrawImage(srcImg, new SKRectI(0, 0, resizedWidth, resizedHeight),
        paint);
    surface.Canvas.Flush();

    using (var newImg = surface.Snapshot())
    {
        // do something with newImg
    }
}