SixLabors/Samples

How to set the image rounded transparent background ?

t-jian opened this issue · 2 comments

I followed the example given by github to generate the circular image.
However, the generated image has a blac background after being exported
How do I set a transparent background?
var logoRequest = (HttpWebRequest)WebRequest.Create("https://vip.ztan.net/upload/image/2023-0209/384134265131077.png"); HttpWebResponse logoResponse = (HttpWebResponse)logoRequest.GetResponse(); using Stream logoResponseStream = logoResponse.GetResponseStream(); var logo = Image.Load(logoResponseStream); logo= logo.Clone(x => x.ConvertToAvatar(new Size(80, 80), 80 / 2)); using var stream = new MemoryStream(); await logo.SaveAsync(stream, new PngEncoder()); stream.Position = 0; return stream.ToArray();
ConvertToAvatar code refer to https://github.com/SixLabors/Samples/blob/main/ImageSharp/AvatarWithRoundedCorner/Program.cs

c2896e5d-ff66-480a-b6e2-d45fec0f8187

Make sure you are using a pixel format that supports an alpha component and also ensure the encoder uses a color type that supports an alpha component. Your input image will be 24bit so you need to explicitly override that.

Thank you. I've made it