【问题标题】:SkiaSharp cut out pathSkiaSharp 切出路径
【发布时间】: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


    【解决方案1】:

    我认为您可以通过设置SPaint.BlendMode = SKPaintBlendMode.Src 来做到这一点。这意味着在画布绘制时,它必须使用源颜色,并替换现有颜色。

    https://docs.microsoft.com/dotnet/api/skiasharp.skpaint.blendmode

    你实际上在做什么

    canvas.DrawPath(path, new SKPaint { Color = SKColors.Transparent});
    

    正在拿起画笔,将其浸入透明颜料中,然后进行绘画。所以你什么也看不到。油漆很清晰。

    但是,你更想做的是在 绘图之前剪辑:

    https://docs.microsoft.com/dotnet/api/skiasharp.skcanvas.clippath

    canvas.Clear(SKColors.Transparent);
    
    // create the circle for the picture
    var path = new SKPath();
    path.AddCircle(image.Width / 2, image.Height / 2, image.Width / 2 - 1f);
    
    // tell the canvas not to draw outside the circle
    canvas.ClipPath(path);
    
    // draw the bitmap
    canvas.DrawBitmap(image, new SKPoint(0, 0));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 2017-06-07
      • 2019-08-03
      • 2015-09-16
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多