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:
parent
311b3e76f0
commit
73597689ed
16 changed files with 115 additions and 90 deletions
|
|
@ -73,7 +73,7 @@
|
|||
|
||||
<TextBlock Text="Libreria Immagini" FontWeight="Bold" Margin="0,12,0,0" />
|
||||
<StackPanel Orientation="Horizontal" Margin="0,6,0,0">
|
||||
<RadioButton Content="System.Graphics" IsChecked="{Binding UseSystemGraphics}" GroupName="Lib" />
|
||||
<RadioButton Content="System.Graphics" IsChecked="{Binding UseSystemGraphics}" GroupName="Lib" IsVisible="{Binding IsRunningOnWindows}" />
|
||||
<RadioButton Content="ImageSharp" IsChecked="{Binding UseImageSharp}" GroupName="Lib" Margin="8,0,0,0" />
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
|
|
|
|||
|
|
@ -5,12 +5,17 @@ using System.Collections.Concurrent;
|
|||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
#if WINDOWS
|
||||
using System.Drawing.Text;
|
||||
#endif
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#if WINDOWS
|
||||
using System.Windows.Forms;
|
||||
#endif
|
||||
using System.Windows.Input;
|
||||
using AutoMapper;
|
||||
using MaddoShared;
|
||||
|
|
@ -258,12 +263,16 @@ namespace ImageCatalog_2
|
|||
|
||||
private List<string> LoadAvailableFonts()
|
||||
{
|
||||
#if WINDOWS
|
||||
var fonts = new List<string>();
|
||||
using (var installedFonts = new InstalledFontCollection())
|
||||
{
|
||||
fonts.AddRange(installedFonts.Families.Select(f => f.Name));
|
||||
}
|
||||
return fonts;
|
||||
#else
|
||||
return new List<string>();
|
||||
#endif
|
||||
}
|
||||
|
||||
private CancellationTokenSource? _mainToken;
|
||||
|
|
@ -604,7 +613,12 @@ namespace ImageCatalog_2
|
|||
}
|
||||
|
||||
// Image library selection (UI radio buttons bind to the boolean helpers)
|
||||
private string _imageLibrary = "System.Graphics";
|
||||
private string _imageLibrary = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "System.Graphics" : "ImageSharp";
|
||||
|
||||
/// <summary>
|
||||
/// Whether the application is running on Windows. Used by cross-platform UIs to show/hide Windows-only options.
|
||||
/// </summary>
|
||||
public bool IsRunningOnWindows => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
||||
|
||||
/// <summary>
|
||||
/// The selected image processing library. Possible values: "System.Graphics" or "ImageSharp".
|
||||
|
|
@ -624,6 +638,7 @@ namespace ImageCatalog_2
|
|||
NotifyPropertyChanged();
|
||||
NotifyPropertyChanged(nameof(UseSystemGraphics));
|
||||
NotifyPropertyChanged(nameof(UseImageSharp));
|
||||
NotifyPropertyChanged(nameof(IsRunningOnWindows));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1506,7 +1521,11 @@ namespace ImageCatalog_2
|
|||
public event EventHandler<string> LoadSettingsRequested;
|
||||
public event EventHandler SelectColorRequested;
|
||||
// Request that the View shows a message to the user (message, caption, icon)
|
||||
#if WINDOWS
|
||||
public event EventHandler<Tuple<string, string, MessageBoxIcon>> ShowMessageRequested;
|
||||
#else
|
||||
public event EventHandler<Tuple<string, string, int>> ShowMessageRequested;
|
||||
#endif
|
||||
public event EventHandler SelectTransparentColorRequested;
|
||||
|
||||
private void SelectSourceFolder(object parameter)
|
||||
|
|
|
|||
|
|
@ -1,19 +1,24 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net10.0-windows</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UseWindowsForms>true</UseWindowsForms>
|
||||
<UseWPF>true</UseWPF>
|
||||
<ProduceReferenceAssembly>False</ProduceReferenceAssembly>
|
||||
<!-- Default assembly name for regular builds -->
|
||||
<AssemblyName>ImageCatalog</AssemblyName>
|
||||
<LangVersion>default</LangVersion>
|
||||
</PropertyGroup>
|
||||
<!-- Windows: net10.0-windows TFM auto-defines WINDOWS preprocessor symbol -->
|
||||
<PropertyGroup Condition="$([MSBuild]::IsOsPlatform('Windows'))">
|
||||
<TargetFramework>net10.0-windows</TargetFramework>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<UseWindowsForms>true</UseWindowsForms>
|
||||
<UseWPF>true</UseWPF>
|
||||
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
|
||||
<ApplicationIcon>Logo.ico</ApplicationIcon>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
|
||||
<PropertyGroup Condition="!$([MSBuild]::IsOsPlatform('Windows'))">
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<OutputType>Exe</OutputType>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<!-- Keep MinVer package enabled but do NOT let it overwrite AssemblyVersion/FileVersion used at build-time.
|
||||
|
|
@ -42,11 +47,11 @@
|
|||
<ItemGroup>
|
||||
<PackageReference Include="AIFotoONLUS.Core" Version="0.1.1" />
|
||||
<PackageReference Include="AutoMapper" Version="16.0.0" />
|
||||
<PackageReference Include="MahApps.Metro" Version="2.4.11" />
|
||||
<PackageReference Include="MahApps.Metro" Version="2.4.11" Condition="$([MSBuild]::IsOsPlatform('Windows'))" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.3" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.3" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="10.0.3" />
|
||||
<PackageReference Include="MahApps.Metro.IconPacks" Version="6.2.1" />
|
||||
<PackageReference Include="MahApps.Metro.IconPacks" Version="6.2.1" Condition="$([MSBuild]::IsOsPlatform('Windows'))" />
|
||||
<PackageReference Include="MinVer" Version="7.0.0" PrivateAssets="all" />
|
||||
<PackageReference Include="Avalonia" Version="11.3.0" />
|
||||
<PackageReference Include="Avalonia.Desktop" Version="11.3.0" />
|
||||
|
|
|
|||
6
imagecatalog/MainForm.Designer.cs
generated
6
imagecatalog/MainForm.Designer.cs
generated
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
#if WINDOWS
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
|
@ -2241,4 +2242,5 @@ namespace ImageCatalog
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
#if WINDOWS
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
|
|
@ -837,4 +838,5 @@ public class PicInfo
|
|||
DirDestStart = Dir_DestStart;
|
||||
NomeImmagine = Nome_Immagine;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
#if WINDOWS
|
||||
using System.Windows;
|
||||
using MahApps.Metro.Controls;
|
||||
using ControlzEx.Theming;
|
||||
|
|
@ -378,3 +379,5 @@ namespace ImageCatalog_2
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -195,7 +195,7 @@ namespace ImageCatalog_2.Models
|
|||
// Selected image processing library (e.g., "System.Graphics" or "ImageSharp")
|
||||
[JsonPropertyName("ImageLibrary")]
|
||||
[XmlElement("ImageLibrary")]
|
||||
public string ImageLibrary { get; set; } = "System.Graphics";
|
||||
public string ImageLibrary { get; set; } = "ImageSharp";
|
||||
|
||||
// Options
|
||||
[JsonPropertyName("ForceJpeg")]
|
||||
|
|
|
|||
|
|
@ -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 =>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
|
|
@ -6,14 +6,18 @@ using System.Runtime.CompilerServices;
|
|||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#if WINDOWS
|
||||
using System.Windows.Forms;
|
||||
#endif
|
||||
|
||||
namespace ImageCatalog_2
|
||||
{
|
||||
public class ViewModelBase : INotifyPropertyChanged
|
||||
{
|
||||
private readonly SynchronizationContext? _synchronizationContext;
|
||||
#if WINDOWS
|
||||
private Control? _control;
|
||||
#endif
|
||||
|
||||
protected ViewModelBase()
|
||||
{
|
||||
|
|
@ -25,10 +29,12 @@ namespace ImageCatalog_2
|
|||
/// Set a Control to use for thread marshalling in WinForms applications.
|
||||
/// This is required for proper cross-thread handling with data binding.
|
||||
/// </summary>
|
||||
#if WINDOWS
|
||||
public void SetControl(Control control)
|
||||
{
|
||||
_control = control;
|
||||
}
|
||||
#endif
|
||||
|
||||
public event PropertyChangedEventHandler? PropertyChanged;
|
||||
|
||||
|
|
@ -40,6 +46,7 @@ namespace ImageCatalog_2
|
|||
if (PropertyChanged == null)
|
||||
return;
|
||||
|
||||
#if WINDOWS
|
||||
// If we have a Control reference (WinForms), use Control.Invoke for proper marshalling
|
||||
if (_control != null)
|
||||
{
|
||||
|
|
@ -53,7 +60,9 @@ namespace ImageCatalog_2
|
|||
}
|
||||
}
|
||||
// Fallback to SynchronizationContext if available
|
||||
else if (_synchronizationContext != null && SynchronizationContext.Current != _synchronizationContext)
|
||||
else
|
||||
#endif
|
||||
if (_synchronizationContext != null && SynchronizationContext.Current != _synchronizationContext)
|
||||
{
|
||||
// We're on a different thread, marshal to the UI thread
|
||||
_synchronizationContext.Send(_ =>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue