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

@ -1,4 +1,4 @@
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using ImageCatalog;
using ImageCatalog_2.Services;
using MaddoShared;
@ -16,6 +16,7 @@ namespace ImageCatalog_2;
static class Program
{
#if WINDOWS
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AllocConsole();
@ -56,6 +57,7 @@ static class Program
Console.SetOut(standardOutput);
Console.SetError(standardOutput);
}
#endif
public static IServiceProvider ServiceProvider { get; private set; }
@ -67,19 +69,20 @@ static class Program
[STAThread]
static void Main(string[] args)
{
System.Windows.Forms.Application.SetHighDpiMode(HighDpiMode.SystemAware);
#if WINDOWS
System.Windows.Forms.Application.SetHighDpiMode(System.Windows.Forms.HighDpiMode.SystemAware);
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
AllocConsole();
RedirectConsoleOutput();
#endif
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
ServiceProvider = serviceCollection.BuildServiceProvider();
// Resolve WPF MainWindow when available, otherwise fall back to WinForms MainForm
var serviceProvider = ServiceProvider;
// Determine UI based on command line. Default: WinForms. Use --wpf for WPF, --avalonia for Avalonia.
@ -92,19 +95,16 @@ static class Program
return;
}
#if WINDOWS
if (useWpf)
{
// Create the WPF Application and merge MahApps resources BEFORE constructing the MainWindow
// so InitializeComponent sees the theme resources on first render.
var wpfApp = new System.Windows.Application();
try
{
wpfApp.Resources.MergedDictionaries.Add(new System.Windows.ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml") });
wpfApp.Resources.MergedDictionaries.Add(new System.Windows.ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml") });
// Default Light theme (can be replaced at runtime)
wpfApp.Resources.MergedDictionaries.Add(new System.Windows.ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml") });
// Also notify ThemeManager about initial theme so chrome and MahApps brushes are applied
try
{
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(wpfApp, "Light.Blue");
@ -116,10 +116,9 @@ static class Program
}
catch
{
// If resources fail to load (package not present at runtime), continue silently
// If resources fail to load, continue silently
}
// Now resolve the WPF MainWindow so its constructor runs with the application resources available
var wpfMain = serviceProvider.GetService(typeof(ImageCatalog_2.MainWindow)) as ImageCatalog_2.MainWindow;
if (wpfMain is not null)
{
@ -127,26 +126,27 @@ static class Program
return;
}
// If WPF was requested but not available, fall back to WinForms.
// If WPF was requested but not available, fall through to WinForms.
}
// Default / fallback to WinForms UI
var mainForm = serviceProvider.GetRequiredService<MainForm>();
System.Windows.Forms.Application.Run(mainForm);
#else
// On non-Windows, Avalonia is the only available UI
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args ?? Array.Empty<string>());
#endif
}
private static void ConfigureServices(ServiceCollection services)
{
// Register AutoMapper (new AddAutoMapper overload — provide config and marker types)
services.AddAutoMapper(cfg => { }, typeof(Program));
// Register your services here
services.AddTransient<ITestService, TestService>();
services.AddTransient<ISettingsService, SettingsService>();
services.AddTransient<DataModel>(sp =>
{
// Resolve optional version provider and pass to DataModel
var testService = sp.GetRequiredService<ITestService>();
var settingsService = sp.GetRequiredService<ISettingsService>();
var imageCreation = sp.GetRequiredService<ImageCreationService>();
@ -159,25 +159,24 @@ static class Program
});
services.AddTransient<ImageCreationService>();
#if WINDOWS
services.AddTransient<ImageCreatorGDI>();
#endif
services.AddTransient<ImageCreatorImageSharp>();
services.AddTransient<ImageCreatorMapper>();
// Register IImageCreator to be resolved via ImageCreatorMapper which selects concrete implementation at call time
services.AddTransient<IImageCreator>(sp => sp.GetRequiredService<ImageCreatorMapper>());
// Register a ParametriSetup singleton that persists user preferences in LocalApplicationData
var userPrefsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"ImageCatalog", "userprefs.xml");
services.AddSingleton(new ParametriSetup(userPrefsPath));
services.AddSingleton<PicSettings>();
// Register your forms
#if WINDOWS
services.AddTransient<MainForm>();
// Register WPF MainWindow so it can be resolved with the existing DataModel
services.AddTransient<ImageCatalog_2.MainWindow>();
#endif
// Version provider for UI and logging
services.AddSingleton<MaddoShared.IVersionProvider, MaddoShared.VersionProvider>();
services.AddLogging(configure =>