58 lines
1.7 KiB
C#
58 lines
1.7 KiB
C#
using Sarand.Net.Standard.Base.App;
|
|
using SixLabors.ImageSharp;
|
|
using SixLabors.ImageSharp.PixelFormats;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace Sarand.Net.Standard.Base.Utils
|
|
{
|
|
//=======================================================================
|
|
// Image handling methods utilising SixLabors ImageSharp package.
|
|
//=======================================================================
|
|
public static class ImageUtils
|
|
{
|
|
//-------------------------------------------------------------------
|
|
private static readonly String className = typeof(ImageUtils).ToString();
|
|
|
|
//-------------------------------------------------------------------
|
|
public class ImageConfig
|
|
{
|
|
public byte[] ImageData { get; set; }
|
|
public bool ValidImage { get; set; }
|
|
public int Width { get; set; }
|
|
public int Height { get; set; }
|
|
}
|
|
public static ImageConfig VerifyImageData(byte[] imageData)
|
|
{
|
|
String fn = className + ".VerifyImageData";
|
|
ImageConfig imageConfig = new ImageConfig();
|
|
imageConfig.ImageData = imageData;
|
|
try
|
|
{
|
|
var image = Image.Load<Rgba32>(imageData);
|
|
if (image == null) return imageConfig;
|
|
imageConfig.Width = image.Width;
|
|
imageConfig.Height = image.Height;
|
|
imageConfig.ValidImage = true;
|
|
return imageConfig;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SaLogger.Warn(fn + " " + ExceptionUtil.GetLogMessage(exc));
|
|
}
|
|
return imageConfig;
|
|
}
|
|
|
|
// Load from byte array.
|
|
public static Image LoadImage(byte[] imageData)
|
|
{
|
|
var image = Image.Load<Rgba32>(imageData);
|
|
return image;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|