Skip to content

How to create a transparency mask? #2462

Closed Answered by ian-cameron
ian-cameron asked this question in Q&A
Discussion options

You must be logged in to vote

I think I figured it out after seeing the article about pixel buffers... so I'll put it here for anyone else, or if there is a better way to accomplish it let me know. Thank you.

using (Image<Rgba32> image = Image.Load<Rgba32>(mask))
{
    image.ProcessPixelRows(accessor =>
    {
        for (int y = 0; y < accessor.Height; y++)
        {
            Span<Rgba32> pixelRow = accessor.GetRowSpan(y);
            foreach (ref Rgba32 pixel in pixelRow)
                pixel = pixel.A == 0 ? Color.Transparent : Color.Black;
        }
    });
    image.SaveAsPng(maskPath);
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ian-cameron
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant