Png to Webp with MatteColor will throw Unsupported pixel format exception
Prerequisite: A PNG image with alpha transparency.
The code to reproduce this issue:
using System.Drawing;
using PhotoSauce.MagicScaler;
using PhotoSauce.NativeCodecs.Giflib;
using PhotoSauce.NativeCodecs.Libheif;
using PhotoSauce.NativeCodecs.Libjpeg;
using PhotoSauce.NativeCodecs.Libjxl;
using PhotoSauce.NativeCodecs.Libpng;
using PhotoSauce.NativeCodecs.Libwebp;
CodecManager.Configure(codecs =>
{
codecs.UseGiflib();
codecs.UseLibheif();
codecs.UseLibjpeg();
codecs.UseLibpng();
codecs.UseLibjxl();
codecs.UseLibwebp();
});
var setting = new ProcessImageSettings
{
MatteColor = Color.White
};
setting.Width = 1000;
setting.Height = 1000;
setting.TrySetEncoderFormat("image/webp");
using var ms= new MemoryStream();
var source = new MemoryStream(File.ReadAllBytes("C:\\t\\Test222.png"));
source.Position = 0;
MagicImageProcessor.ProcessImage(source, ms, setting);
using var fs= File.Create("C:\\t\\test.webp");
ms.Position = 0;
ms.CopyTo(fs);
fs.Flush();
Console.WriteLine("Finished");
if remove MatteColor = Color.White, it will work fine.
use latest package from azure:
<ItemGroup>
<PackageReference Include="PhotoSauce.NativeCodecs.Giflib" Version="5.2.2-ci242323" />
<PackageReference Include="PhotoSauce.NativeCodecs.Libheif" Version="1.18.2-ci242323" />
<PackageReference Include="PhotoSauce.NativeCodecs.Libjpeg" Version="3.0.3-ci242323" />
<PackageReference Include="PhotoSauce.NativeCodecs.Libjxl" Version="0.10.3-ci242323" />
<PackageReference Include="PhotoSauce.NativeCodecs.Libpng" Version="1.6.43-ci242323" />
<PackageReference Include="PhotoSauce.NativeCodecs.Libwebp" Version="1.4.0-ci242323" />
</ItemGroup>
also happened.
Thanks for the report, but I'm not able to reproduce this after having tried lots of transparent PNG inputs. Can you provide an input image that fails, as well as your environment info?
Hi @saucecontrol
Sure, here is my code and example image, you could test it
Code:
` CodecManager.Configure(codecs =>
{
codecs.UseGiflib();
codecs.UseLibheif();
codecs.UseLibjpeg();
codecs.UseLibpng();
codecs.UseLibjxl();
codecs.UseLibwebp();
});
var setting = new ProcessImageSettings
{
//MatteColor = Color.White => if set color white, it will throw exception
};
setting.Width = 1000;
setting.Height = 1000;
setting.TrySetEncoderFormat("image/webp");
using var ms = new MemoryStream();
var source = new MemoryStream(File.ReadAllBytes("C:\\Users\\Rowan\\Pictures\\Logo2\\Test222.png"));
source.Position = 0;
MagicImageProcessor.ProcessImage(source, ms, setting);
using var fs = File.Create("C:\\Users\\Rowan\\Pictures\\Logo2\\Result.webp");
ms.Position = 0;
ms.CopyTo(fs);
fs.Flush();
Console.WriteLine("Finished");`
Exception:
Example:
Thanks @fengqiaoyexia, I see the issue now.
This is fixed in v0.15.0. Thanks again for the report!