Cross-platform: remove System.Drawing deps, add #if WINDOWS

Refactored image creation APIs to use byte[] for logo data instead of System.Drawing.Image, enabling cross-platform support. Wrapped all GDI+/Windows-specific code in #if WINDOWS and updated project files to conditionally include Windows-only dependencies. Defaulted to ImageSharp on non-Windows, and updated UI and settings to reflect platform capabilities. Application now builds and runs on Linux/macOS with Avalonia and ImageSharp, while retaining full Windows functionality.
This commit is contained in:
MaddoScientisto 2026-02-26 19:17:23 +01:00
commit 73597689ed
16 changed files with 115 additions and 90 deletions

View file

@ -3,7 +3,6 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
@ -14,7 +13,6 @@ using Microsoft.Extensions.Logging;
namespace MaddoShared
{
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
public class ImageCreationService(
ILogger<ImageCreationService> logger,
PicSettings picSettings,
@ -72,19 +70,15 @@ namespace MaddoShared
// int threads = options.MaxThreads == 0 ? Environment.ProcessorCount * 2 : options.MaxThreads;
int threads = options.MaxThreads;
Bitmap logoBmp = null;
// Load Logo (short-circuit)
// Load logo once as raw bytes (cross-platform). byte[] is safe to share across threads.
byte[]? logoBytes = null;
if (picSettings.LogoAggiungi && File.Exists(picSettings.LogoNomeFile))
{
logoBmp = new Bitmap(picSettings.LogoNomeFile);
logoBytes = File.ReadAllBytes(picSettings.LogoNomeFile);
}
Func<FileData, Task> processFile = async fileData =>
{
Bitmap logoCopy = logoBmp is null
? null
: logoBmp.Clone(new Rectangle(0, 0, logoBmp.Width, logoBmp.Height), logoBmp.PixelFormat);
var imgState = new ImageState
{
WorkFile = fileData.File,
@ -93,8 +87,7 @@ namespace MaddoShared
try
{
// Ensure CreateImageAsync can accept a null logoCopy value.
await imageCreatorService.CreateImageAsync(imgState, logoCopy);
await imageCreatorService.CreateImageAsync(imgState, logoBytes);
results.Add(fileData.File.Name);
@ -110,8 +103,7 @@ namespace MaddoShared
}
finally
{
// Dispose the clone if it was created
logoCopy?.Dispose();
// nothing to dispose — byte[] is managed
}
};
@ -139,14 +131,6 @@ namespace MaddoShared
}
}
try
{
logoBmp?.Dispose();
}
catch (Exception e)
{
logger.LogError(e, "Error in disposing the logo");
}
}
private List<FileData> GetFilesToProcess(Options options)