ImageCodecInfoHelper
using System.Runtime.CompilerServices;
using Windows.Win32;
using Windows.Win32.Graphics.GdiPlus;
namespace System.Drawing
{
internal static class ImageCodecInfoHelper
{
[Nullable(new byte[] {
2,
0
})]
private static (Guid Format, Guid Encoder)[] s_encoders;
[Nullable(new byte[] {
1,
0
})]
private unsafe static (Guid Format, Guid Encoder)[] Encoders {
[return: Nullable(new byte[] {
1,
0
})]
get {
if (s_encoders == null) {
GdiPlusInitialization.EnsureInitialized();
uint num = default(uint);
uint num2 = default(uint);
PInvokeGdiPlus.GdipGetImageEncodersSize(&num, &num2).ThrowIfFailed();
BufferScope<byte> bufferScope = new BufferScope<byte>((int)num2);
try {
fixed (byte* ptr = &bufferScope.GetPinnableReference()) {
PInvokeGdiPlus.GdipGetImageEncoders(num, num2, (ImageCodecInfo*)ptr).ThrowIfFailed();
ReadOnlySpan<ImageCodecInfo> readOnlySpan = new ReadOnlySpan<ImageCodecInfo>(ptr, (int)num);
s_encoders = new(Guid, Guid)[readOnlySpan.Length];
for (int i = 0; i < readOnlySpan.Length; i++) {
s_encoders[i] = (readOnlySpan[i].FormatID, readOnlySpan[i].Clsid);
}
}
} finally {
bufferScope.Dispose();
}
}
return s_encoders;
}
}
internal static Guid GetEncoderClsid(Guid format)
{
(Guid, Guid)[] encoders = Encoders;
foreach ((Guid, Guid) valueTuple in encoders) {
Guid item = valueTuple.Item1;
Guid item2 = valueTuple.Item2;
if (item == format)
return item2;
}
return Guid.Empty;
}
}
}