28 ArgumentNullException.ThrowIfNull(stream);
29 ImageInfo info = SixLabors.ImageSharp.Image.Identify(stream);
30 if (info.Metadata.DecodedImageFormat.Name !=
"PNG") {
31 throw new FormatException($
"Image format({info.Metadata.DecodedImageFormat.Name}) is not Png");
34 result.Width = info.
Width;
35 result.Height = info.Height;
36 result.Format = info.Metadata.GetPngMetadata().ColorType
switch {
37 PngColorType.RgbWithAlpha =>
Format.RGBA8,
38 PngColorType.Rgb =>
Format.RGB8,
39 PngColorType.GrayscaleWithAlpha =>
Format.LA8,
40 PngColorType.Grayscale =>
Format.L8,
41 PngColorType.Palette =>
Format.Indexed,
42 _ =>
throw new InvalidOperationException(
"Unsupported PNG pixel format.")
56 PngCompressionLevel compressionLevel = PngCompressionLevel.DefaultCompression,
58 ArgumentNullException.ThrowIfNull(image);
59 ArgumentNullException.ThrowIfNull(stream);
60 PngColorType pngColorType;
62 case Format.RGBA8: pngColorType = PngColorType.RgbWithAlpha;
break;
63 case Format.RGB8: pngColorType = PngColorType.Rgb;
break;
64 case Format.LA8: pngColorType = PngColorType.GrayscaleWithAlpha;
break;
65 case Format.L8: pngColorType = PngColorType.Grayscale;
break;
66 case Format.Indexed: pngColorType = PngColorType.Palette;
break;
67 default:
throw new InvalidOperationException(
"Unsupported PNG pixel format.");
69 PngEncoder encoder =
new() {
70 ColorType = pngColorType, CompressionLevel = compressionLevel, TransparentColorMode = PngTransparentColorMode.Clear