Conversion d'image avec interface et base64 #92

Merged
lucas.duflot merged 1 commits from converterToBase64 into dev 11 months ago

@ -0,0 +1,29 @@
using System;
using Models.Interfaces;
namespace Models.Game
{
/// <summary>
/// Converter to Base64
/// </summary>
public class Base64Converter : IImageConverter
{
/// <summary>
/// Converts an image to a base64 string
/// </summary>
/// <param name="imagePath">The path to access the image</param>
/// <returns>The base64 string representation of the image</returns>
/// <exception cref="FileNotFoundException">Native .NET exception</exception>
public string ConvertImage(string imagePath)
{
if (!File.Exists(imagePath))
{
// native .NET exception
throw new FileNotFoundException("Image file not found", imagePath);
}
byte[] imageBytes = File.ReadAllBytes(imagePath);
return Convert.ToBase64String(imageBytes);
}
}
}

@ -0,0 +1,15 @@
namespace Models.Interfaces
{
/// <summary>
/// Interface for image converters
/// </summary>
public interface IImageConverter
{
/// <summary>
/// Converter to any type that can be converted to a string (ex: Base64)
/// </summary>
/// <param name="imagePath"></param>
/// <returns></returns>
string ConvertImage(string imagePath);
}
}

@ -7,7 +7,6 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Game\" />
<Folder Include="Rules\" /> <Folder Include="Rules\" />
</ItemGroup> </ItemGroup>

Loading…
Cancel
Save