ROIEditor/ImagenEstado.cs

82 lines
2.4 KiB
C#
Raw Normal View History

2024-04-11 06:51:12 -03:00
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
namespace ROIEditor
{
public class ImagenEstado
{
public double ZoomScaleX { get; set; }
public double ZoomScaleY { get; set; }
public double HorizontalOffset { get; set; }
public double VerticalOffset { get; set; }
public string NombreImagen;
// Constructor sin parámetros
public ImagenEstado()
{
}
public ImagenEstado(string imageNameActual)
{
NombreImagen = imageNameActual;
}
public string PathPlantillasJson()
{
string jsonPath = Path.ChangeExtension(NombreImagen, ".json");
var imagePath = Path.Combine(Directory.GetCurrentDirectory(), "Plantillas", jsonPath);
return imagePath;
}
public string PathPlantillasPNG()
{
string jsonPath = Path.ChangeExtension(NombreImagen, ".png");
var imagePath = Path.Combine(Directory.GetCurrentDirectory(), "Plantillas", jsonPath);
return imagePath;
}
public void SalvarEstadoJSON()
{
// Cambia la extensión del archivo de imagen a .json
string jsonPath = PathPlantillasJson();
// Serializa el estado de la imagen a JSON
string json = JsonConvert.SerializeObject(NombreImagen, Formatting.Indented);
// Escribe el JSON en un archivo
File.WriteAllText(jsonPath, json);
}
public void CargarEstadoJSON()
{
string jsonPath = PathPlantillasJson();
if (File.Exists(jsonPath))
{
string json = File.ReadAllText(jsonPath);
ImagenEstado estadoCargado = JsonConvert.DeserializeObject<ImagenEstado>(json);
// Usar reflexión para copiar todas las propiedades
foreach (PropertyInfo prop in typeof(ImagenEstado).GetProperties())
{
if (prop.CanRead && prop.CanWrite)
{
prop.SetValue(this, prop.GetValue(estadoCargado, null), null);
}
}
}
}
}
}