Actually changing pixel transparency during Mutate #2185
-
Right, I am getting my Image instance like so:
I then save this as a PNG to load it into a XAML Image element... Works great, until I want to replace with #00000000, which does not happen... all other colors are fine, just the transparency bit is ignored. Any ideas where I mess this up? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 29 replies
-
Sounds like you're not saving the png in 32bit format. |
Beta Was this translation helpful? Give feedback.
-
I can't seem to replicate your issue. Here's some code demonstrating working with bitmaps of varying bit depth. Note it would be more efficient to use Vector4 for rather than converting to/from using Dicussion2185;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
Console.WriteLine("Hello, World!");
var appPath = Path.Combine(Directory.GetCurrentDirectory(), @"..\..\..\");
var depths = new int[] { 4, 8, 24 };
foreach (var depth in depths)
{
using var image = Image.Load(Path.Combine(appPath, $"input{depth}.bmp"));
image.Mutate(c => c.ProcessPixelRowsAsVector4(row =>
{
Rgba32 sourceColor = default;
Rgba32 matchColor = Color.White;
Rgba32 target = default;
for (int x = 0; x < row.Length; x++)
{
// Get
sourceColor.R = (byte)(255F * row[x].X);
sourceColor.G = (byte)(255F * row[x].Y);
sourceColor.B = (byte)(255F * row[x].Z);
sourceColor.A = (byte)(255F * row[x].W);
// What is the distance?
double distance = ColorHelper.Distance(sourceColor, matchColor);
// Match up to a diff of 1 per pixel component.
if (distance <= 1.74)
{
// SET
row[x].X = target.R / 255F;
row[x].Y = target.G / 255F;
row[x].Z = target.B / 255F;
row[x].W = target.A / 255F;
}
}
}, PixelConversionModifiers.Scale));
string outPath = Path.GetFullPath(Path.Combine(appPath, $"output{depth}.png"));
image.Save(Path.Combine(appPath, $"output{depth}.png"), new PngEncoder());
Console.WriteLine(outPath);
} Here the actual working example. |
Beta Was this translation helpful? Give feedback.
-
is |
Beta Was this translation helpful? Give feedback.
I can't seem to replicate your issue. Here's some code demonstrating working with bitmaps of varying bit depth.
Note it would be more efficient to use Vector4 for rather than converting to/from
Rgba32
, in addition to the lack of converting overhead (which these methods are designed to avoid) you could then pow2 your distance threshold and usedVector4.DistanceSquared(...)
to get the difference across all components faster.