【发布时间】:2020-07-01 12:56:12
【问题描述】:
我正在使用 System.Drawing 进行一些图像编辑,现在将所有内容移植到 SkiaSharp 以便在 Linux / .NET Core 上使用它。一切正常,除了我还没有找到一种以编程方式为图像提供圆角的方法。
我编写了一些代码,它依赖于以圆形形式绘制路径,然后尝试将路径外部着色为透明。这不起作用,因为它似乎有多个层,并且使上层的部分透明不会使图像的整个区域(所有层)透明。这是我的代码:
public static SKBitmap MakeImageRound(SKBitmap image)
{
SKBitmap finishedImage = new SKBitmap(image.Width, image.Height);
using (SKCanvas canvas = new SKCanvas(finishedImage))
{
canvas.Clear(SKColors.Transparent);
canvas.DrawBitmap(image, new SKPoint(0, 0));
SKPath path = new SKPath();
path.AddCircle(image.Width / 2, image.Height / 2, image.Width / 2 - 1f);
path.FillType = SKPathFillType.InverseEvenOdd;
path.Close();
canvas.DrawPath(path, new SKPaint {Color = SKColors.Transparent, Style = SKPaintStyle.Fill });
canvas.ResetMatrix();
return finishedImage;
}
}
如果这是糟糕的代码,我很抱歉,这是我在 C# 中进行图像编辑的第一次体验,因此我也是 SkiaSharp 的绝对初学者。我修改了从here 获得的 System.Drawing 代码。
我也看了这个Microsoft document。它显示使用路径进行剪辑,但我还不能让它工作。
总之:我正在寻找一种方法来使图像/画布的所有层在某些区域透明。
非常感谢任何帮助! :D
【问题讨论】:
标签: c# .net .net-core skiasharp