Aggiunta in API export doorImage (SVG+PNG)
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NLog;
|
||||
using Svg;
|
||||
using System.Data.SqlTypes;
|
||||
using System.Drawing;
|
||||
using WebDoorCreator.Data.Services;
|
||||
|
||||
namespace WebDoorCreator.API.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class DoorImageController : ControllerBase
|
||||
{
|
||||
#region Public Constructors
|
||||
|
||||
public DoorImageController(IConfiguration configuration, QueueDataService DataService)
|
||||
{
|
||||
Log.Info("Starting DoorImageController");
|
||||
_configuration = configuration;
|
||||
QDataServ = DataService;
|
||||
Log.Info("Avviato DoorImageController");
|
||||
}
|
||||
|
||||
#endregion Public Constructors
|
||||
|
||||
#region Public Methods
|
||||
|
||||
[HttpGet("GetImage.png")]
|
||||
public async Task<IActionResult> GetImagePng(int DoorId)
|
||||
{
|
||||
byte[] result= new byte[0];
|
||||
string svgContent = await QDataServ.DoorGetLastSvg(DoorId);
|
||||
if (!string.IsNullOrEmpty(svgContent))
|
||||
{
|
||||
var mySvg = SvgDocument.FromSvg<SvgDocument>(svgContent);
|
||||
//result = System.Text.Encoding.UTF8.GetBytes(svgContent);
|
||||
var myBmp = mySvg.Draw();
|
||||
result = ImageToByte2(myBmp);
|
||||
}
|
||||
return File(result, "image/png");
|
||||
}
|
||||
|
||||
[HttpGet("GetImage.svg")]
|
||||
public async Task<IActionResult> GetImageSvg(int DoorId)
|
||||
{
|
||||
string svgContent = await QDataServ.DoorGetLastSvg(DoorId);
|
||||
var result = System.Text.Encoding.UTF8.GetBytes(svgContent);
|
||||
return File(result, "image/svg+xml");
|
||||
}
|
||||
|
||||
#endregion Public Methods
|
||||
|
||||
#region Private Fields
|
||||
|
||||
private static IConfiguration _configuration = null!;
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
#endregion Private Fields
|
||||
|
||||
#region Private Properties
|
||||
|
||||
private QueueDataService QDataServ { get; set; } = null!;
|
||||
|
||||
private byte[] ImageToByte2(Image img)
|
||||
{
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Private Properties
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user