3

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

我问这个是因为如果我并行执行这些函数(在我的例子中大约 60 个线程),每个单个缩放函数的执行时间会增加多达 20 倍。

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

方法一:

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

方法二:

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();
}
4

1 回答 1

2

这是我使用的代码。另一个想法是确保将您的 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
    }
}
于 2018-05-15T07:23:24.167 回答