Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
Png.cs
浏览该文件的文档.
1using SixLabors.ImageSharp;
2using SixLabors.ImageSharp.Formats.Png;
3
4namespace Engine.Media {
5 public static class Png {
6 public enum Format {
12 }
13
14 public struct PngInfo {
15 public int Width;
16
17 public int Height;
18
19 public Format Format;
20 }
21
22 public static bool IsPngStream(Stream stream) {
23 ArgumentNullException.ThrowIfNull(stream);
24 return SixLabors.ImageSharp.Image.DetectFormat(stream).Name == "PNG";
25 }
26
27 public static PngInfo GetInfo(Stream stream) {
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");
32 }
33 PngInfo result = default;
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.")
43 };
44 return result;
45 }
46
47 public static Image Load(Stream stream) {
48 ArgumentNullException.ThrowIfNull(stream);
49 string formatName = SixLabors.ImageSharp.Image.DetectFormat(stream).Name;
50 return formatName != "PNG" ? throw new FormatException($"Image format({formatName}) is not Png") : Image.Load(stream);
51 }
52
53 public static void Save(Image image,
54 Stream stream,
55 Format format,
56 PngCompressionLevel compressionLevel = PngCompressionLevel.DefaultCompression,
57 bool sync = false) {
58 ArgumentNullException.ThrowIfNull(image);
59 ArgumentNullException.ThrowIfNull(stream);
60 PngColorType pngColorType;
61 switch (format) {
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.");
68 }
69 PngEncoder encoder = new() {
70 ColorType = pngColorType, CompressionLevel = compressionLevel, TransparentColorMode = PngTransparentColorMode.Clear
71 };
72 if (sync) {
73 image.m_trueImage.SaveAsPng(stream, encoder);
74 }
75 else {
76 image.m_trueImage.SaveAsPngAsync(stream, encoder);
77 }
78 }
79 }
80}
static Image Load(Stream stream, ImageFileFormat format)
readonly Image< Rgba32 > m_trueImage
static Image Load(Stream stream)
定义 Png.cs:47
static void Save(Image image, Stream stream, Format format, PngCompressionLevel compressionLevel=PngCompressionLevel.DefaultCompression, bool sync=false)
定义 Png.cs:53
static PngInfo GetInfo(Stream stream)
定义 Png.cs:27
static bool IsPngStream(Stream stream)
定义 Png.cs:22