Skip to content

How can I make an image transparent? #1272

Discussion options

You must be logged in to vote

There's a few ways:

  1. If your image is not antialiased you could loop through the pixels and replace each instance of Color.White with Color.Transparent.
  2. Do the above but with some sort of threshold to capture variance.
  3. Use the ImageSharp.Drawing package and RecolorBrush which takes source and target colors. You then pass that to the Fill method.

I think 3 is probably the easiest way to go, bear in mind though that ImageSharp.Drawing is a beta and subject to change.

using var image = Image.Load(...);

float threshold = 0..1F;
Color sourceColor = Color.White;
Color targetColor = Color.Transparent;
var brush = new RecolorBrush(sourceColor, targetColor, threshold);

image.Mutate(x => x.Fill(b…

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@Euynac
Comment options

@JimBobSquarePants
Comment options

@Euynac
Comment options

@JimBobSquarePants
Comment options

@mamdos
Comment options

Answer selected by markrendle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants