2025-11-09 15:15:52 +01:00
|
|
|
using ElectronNET.API.Serialization;
|
2025-11-09 12:05:07 +01:00
|
|
|
using System;
|
2020-05-06 19:05:21 -04:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO;
|
2025-11-09 12:05:07 +01:00
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Json.Serialization;
|
2020-05-06 19:05:21 -04:00
|
|
|
|
|
|
|
|
namespace ElectronNET.API.Entities
|
|
|
|
|
{
|
2025-11-22 02:16:10 +01:00
|
|
|
/// <yremarks>Project-specific: JSON converter for NativeImage; no MCP structure equivalent.</yremarks>
|
2025-11-09 12:05:07 +01:00
|
|
|
internal class NativeImageJsonConverter : JsonConverter<NativeImage>
|
2020-05-06 19:05:21 -04:00
|
|
|
{
|
2025-11-09 12:05:07 +01:00
|
|
|
public override void Write(Utf8JsonWriter writer, NativeImage value, JsonSerializerOptions options)
|
2020-05-06 19:05:21 -04:00
|
|
|
{
|
2025-11-09 12:05:07 +01:00
|
|
|
if (value is null)
|
2020-05-06 19:05:21 -04:00
|
|
|
{
|
2025-11-09 12:05:07 +01:00
|
|
|
writer.WriteNullValue();
|
|
|
|
|
return;
|
2020-05-06 19:05:21 -04:00
|
|
|
}
|
2025-11-09 12:05:07 +01:00
|
|
|
|
|
|
|
|
var scaledImages = value.GetAllScaledImages();
|
2025-11-09 15:15:52 +01:00
|
|
|
JsonSerializer.Serialize(writer, scaledImages, ElectronJson.Options);
|
2020-05-06 19:05:21 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-09 12:05:07 +01:00
|
|
|
public override NativeImage Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
2020-05-06 19:05:21 -04:00
|
|
|
{
|
2025-11-09 15:15:52 +01:00
|
|
|
var dict = JsonSerializer.Deserialize<Dictionary<float, string>>(ref reader, ElectronJson.Options);
|
2020-05-06 19:05:21 -04:00
|
|
|
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)));
|
|
|
|
|
}
|
2025-11-09 03:50:24 +01:00
|
|
|
|
2020-05-06 19:05:21 -04:00
|
|
|
return new NativeImage(newDictionary);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-15 08:05:31 +01:00
|
|
|
}
|