mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-02-09 05:34:51 +00:00
39 lines
1.4 KiB
C#
39 lines
1.4 KiB
C#
using ElectronNET.API.Serialization;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace ElectronNET.API.Entities
|
|
{
|
|
/// <yremarks>Project-specific: JSON converter for NativeImage; no MCP structure equivalent.</yremarks>
|
|
internal class NativeImageJsonConverter : JsonConverter<NativeImage>
|
|
{
|
|
public override void Write(Utf8JsonWriter writer, NativeImage value, JsonSerializerOptions options)
|
|
{
|
|
if (value is null)
|
|
{
|
|
writer.WriteNullValue();
|
|
return;
|
|
}
|
|
|
|
var scaledImages = value.GetAllScaledImages();
|
|
JsonSerializer.Serialize(writer, scaledImages, ElectronJson.Options);
|
|
}
|
|
|
|
public override NativeImage Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
var dict = JsonSerializer.Deserialize<Dictionary<float, string>>(ref reader, ElectronJson.Options);
|
|
var newDictionary = new Dictionary<float, Image>();
|
|
foreach (var item in dict)
|
|
{
|
|
var bytes = Convert.FromBase64String(item.Value);
|
|
newDictionary.Add(item.Key, Image.FromStream(new MemoryStream(bytes)));
|
|
}
|
|
|
|
return new NativeImage(newDictionary);
|
|
}
|
|
}
|
|
} |