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,9 +1,8 @@
using System.Drawing;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MaddoShared; namespace MaddoShared;
public interface IImageCreator public interface IImageCreator
{ {
Task CreateImageAsync(ImageState imgState, Image logo); Task CreateImageAsync(ImageState imgState, byte[]? logoData);
} }

View file

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

View file

@ -34,7 +34,7 @@ public class ImageCreatorImageSharp : IImageCreator
_logger = logger; _logger = logger;
} }
public async Task CreateImageAsync(ImageState imgState, System.Drawing.Image logo) public async Task CreateImageAsync(ImageState imgState, byte[]? logoData)
{ {
ArgumentNullException.ThrowIfNull(imgState); ArgumentNullException.ThrowIfNull(imgState);
@ -74,7 +74,7 @@ public class ImageCreatorImageSharp : IImageCreator
var fileNameBig = System.IO.Path.Combine(imgState.DestDir.FullName, imgState.NomeFileBig); var fileNameBig = System.IO.Path.Combine(imgState.DestDir.FullName, imgState.NomeFileBig);
// Draw overlays (text/logo) onto big image using ImageSharp and save // Draw overlays (text/logo) onto big image using ImageSharp and save
await DrawAndSaveWithGdiAsync(imgBig, fileNameBig, imgState, logo, _picSettings.JpegQuality, isThumbnail: false).ConfigureAwait(false); await DrawAndSaveWithGdiAsync(imgBig, fileNameBig, imgState, logoData, _picSettings.JpegQuality, isThumbnail: false).ConfigureAwait(false);
// Create thumbnail if requested // Create thumbnail if requested
if (_picSettings.CreaMiniature) if (_picSettings.CreaMiniature)
@ -85,7 +85,7 @@ public class ImageCreatorImageSharp : IImageCreator
var fileNameSmall = System.IO.Path.Combine(imgState.DestDir.FullName, imgState.NomeFileSmall); var fileNameSmall = System.IO.Path.Combine(imgState.DestDir.FullName, imgState.NomeFileSmall);
// Draw overlays and save thumbnail via ImageSharp // Draw overlays and save thumbnail via ImageSharp
await DrawAndSaveWithGdiAsync(imgSmall, fileNameSmall, imgState, logo, _picSettings.JpegQualityMin, isThumbnail: true).ConfigureAwait(false); await DrawAndSaveWithGdiAsync(imgSmall, fileNameSmall, imgState, logoData, _picSettings.JpegQualityMin, isThumbnail: true).ConfigureAwait(false);
} }
} }
catch (Exception ex) catch (Exception ex)
@ -110,7 +110,7 @@ public class ImageCreatorImageSharp : IImageCreator
}; };
} }
private async Task DrawAndSaveWithGdiAsync(Image<Rgba32> imgSharp, string outputPath, ImageState imgState, System.Drawing.Image logo, long quality, bool isThumbnail) private async Task DrawAndSaveWithGdiAsync(Image<Rgba32> imgSharp, string outputPath, ImageState imgState, byte[]? logoData, long quality, bool isThumbnail)
{ {
// Use ImageSharp drawing APIs to render text and logos and save using ImageSharp encoders. // Use ImageSharp drawing APIs to render text and logos and save using ImageSharp encoders.
// Clone editable image so we don't mutate the original reference unexpectedly. // Clone editable image so we don't mutate the original reference unexpectedly.
@ -287,13 +287,12 @@ public class ImageCreatorImageSharp : IImageCreator
// Draw logo if provided. For compatibility with the original GDI implementation, // Draw logo if provided. For compatibility with the original GDI implementation,
// do not draw the logo on thumbnails (ImageCreatorSharp only draws logos on big images). // do not draw the logo on thumbnails (ImageCreatorSharp only draws logos on big images).
if (logo != null && _picSettings.LogoAggiungi && !isThumbnail) if (logoData != null && logoData.Length > 0 && _picSettings.LogoAggiungi && !isThumbnail)
{ {
try try
{ {
Image<Rgba32> logoImg = null; Image<Rgba32> logoImg = null;
// Prefer configured file if present, otherwise use the provided System.Drawing.Image instance
if (!string.IsNullOrEmpty(_picSettings.LogoNomeFile) && File.Exists(_picSettings.LogoNomeFile)) if (!string.IsNullOrEmpty(_picSettings.LogoNomeFile) && File.Exists(_picSettings.LogoNomeFile))
{ {
using var logoStream = File.OpenRead(_picSettings.LogoNomeFile); using var logoStream = File.OpenRead(_picSettings.LogoNomeFile);
@ -301,10 +300,7 @@ public class ImageCreatorImageSharp : IImageCreator
} }
else else
{ {
// Convert System.Drawing.Image to ImageSharp by saving to PNG in-memory to preserve alpha using var ms = new MemoryStream(logoData);
await using var ms = new MemoryStream();
logo.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
logoImg = await SixLabors.ImageSharp.Image.LoadAsync<Rgba32>(ms).ConfigureAwait(false); logoImg = await SixLabors.ImageSharp.Image.LoadAsync<Rgba32>(ms).ConfigureAwait(false);
} }

View file

@ -7,6 +7,7 @@ namespace MaddoShared;
/// <summary> /// <summary>
/// Dynamically resolves the concrete IImageCreator implementation at call time /// Dynamically resolves the concrete IImageCreator implementation at call time
/// based on current PicSettings.ImageCreatorProvider. /// based on current PicSettings.ImageCreatorProvider.
/// On non-Windows platforms only ImageCreatorImageSharp is available.
/// </summary> /// </summary>
public class ImageCreatorMapper : IImageCreator public class ImageCreatorMapper : IImageCreator
{ {
@ -21,29 +22,33 @@ public class ImageCreatorMapper : IImageCreator
_logger = logger; _logger = logger;
} }
public Task CreateImageAsync(ImageState imgState, System.Drawing.Image logo) public Task CreateImageAsync(ImageState imgState, byte[]? logoData)
{ {
var provider = (_settings.ImageCreatorProvider ?? "Sharp").Trim(); var provider = (_settings.ImageCreatorProvider ?? "Sharp").Trim();
_logger?.LogDebug("Resolving IImageCreator for provider '{Provider}'", provider); _logger?.LogDebug("Resolving IImageCreator for provider '{Provider}'", provider);
#if WINDOWS
return provider.Equals("ALTERNATE", StringComparison.OrdinalIgnoreCase) return provider.Equals("ALTERNATE", StringComparison.OrdinalIgnoreCase)
? ResolveAndCall<ImageCreatorImageSharp>(imgState, logo) ? ResolveAndCall<ImageCreatorImageSharp>(imgState, logoData)
: ResolveAndCall<ImageCreatorGDI>(imgState, logo); : ResolveAndCall<ImageCreatorGDI>(imgState, logoData);
#else
// GDI is not available on non-Windows — always use ImageSharp
return ResolveAndCall<ImageCreatorImageSharp>(imgState, logoData);
#endif
} }
private Task ResolveAndCall<T>(ImageState imgState, System.Drawing.Image logo) where T : IImageCreator private Task ResolveAndCall<T>(ImageState imgState, byte[]? logoData) where T : IImageCreator
{ {
// Resolve the concrete implementation and forward the call var impl = (IImageCreator?)_sp.GetService(typeof(T));
var impl = (IImageCreator)_sp.GetService(typeof(T));
if (impl is null) if (impl is null)
{ {
_logger?.LogWarning("Requested image creator {Type} is not registered. Falling back to ImageCreatorGDI.", typeof(T).Name); _logger?.LogWarning("Requested image creator {Type} is not registered. Falling back to ImageCreatorImageSharp.", typeof(T).Name);
impl = (IImageCreator)_sp.GetService(typeof(ImageCreatorGDI)); impl = (IImageCreator?)_sp.GetService(typeof(ImageCreatorImageSharp));
} }
if (impl is null) if (impl is null)
throw new InvalidOperationException("No IImageCreator implementation is registered."); throw new InvalidOperationException("No IImageCreator implementation is registered.");
return impl.CreateImageAsync(imgState, logo); return impl.CreateImageAsync(imgState, logoData);
} }
} }

View file

@ -1,4 +1,5 @@
using System; #if WINDOWS
using System;
using System.Diagnostics; using System.Diagnostics;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Drawing; using System.Drawing;
@ -17,7 +18,7 @@ namespace MaddoShared;
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")] [SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
public class ImageCreatorGDI(PicSettings picSettings, ILogger<ImageCreatorGDI> logger) : IImageCreator public class ImageCreatorGDI(PicSettings picSettings, ILogger<ImageCreatorGDI> logger) : IImageCreator
{ {
public async Task CreateImageAsync(ImageState imgState, Image logo) public async Task CreateImageAsync(ImageState imgState, byte[]? logoData)
{ {
try try
{ {
@ -51,7 +52,7 @@ public class ImageCreatorGDI(PicSettings picSettings, ILogger<ImageCreatorGDI> l
AddText(g, imgState, imgOutputBig); AddText(g, imgState, imgOutputBig);
AddLogo(imgOutputBig, logo); AddLogo(imgOutputBig, logoData);
SavePhoto(imgOutputBig, imgState, thisFormat); SavePhoto(imgOutputBig, imgState, thisFormat);
}).ConfigureAwait(false); }).ConfigureAwait(false);
@ -480,13 +481,14 @@ public class ImageCreatorGDI(PicSettings picSettings, ILogger<ImageCreatorGDI> l
} }
} }
private void AddLogo(Bitmap imgOutputBig, Image logo) private void AddLogo(Bitmap imgOutputBig, byte[]? logoData)
{ {
// Skip if no logo provided // Skip if no logo bytes provided
if (logo is null) return; if (logoData is null) return;
// Load check (use short-circuit &&) if (!picSettings.LogoAggiungi) return;
if (!(picSettings.LogoAggiungi && File.Exists(picSettings.LogoNomeFile))) return;
using var logo = Image.FromStream(new System.IO.MemoryStream(logoData));
// Decide whether to apply a color-key transparency remap or rely on existing image alpha. // Decide whether to apply a color-key transparency remap or rely on existing image alpha.
// If UseTransparentColor is true, parse the configured TransparentColor and remap it to fully transparent. // If UseTransparentColor is true, parse the configured TransparentColor and remap it to fully transparent.
@ -861,3 +863,4 @@ public class ImageCreatorGDI(PicSettings picSettings, ILogger<ImageCreatorGDI> l
: Environment.NewLine + formatted; : Environment.NewLine + formatted;
} }
} }
#endif // WINDOWS

View file

@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net10.0-windows</TargetFramework> <TargetFramework>net10.0</TargetFramework>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<UseWindowsForms>true</UseWindowsForms>
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
<PlatformTarget>x64</PlatformTarget> <PlatformTarget>x64</PlatformTarget>
<!-- WINDOWS preprocessor symbol mirrors the -windows TFM suffix so #if WINDOWS guards work -->
<DefineConstants Condition="$([MSBuild]::IsOsPlatform('Windows'))">$(DefineConstants);WINDOWS</DefineConstants>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AsyncEnumerator" Version="4.0.2" /> <PackageReference Include="AsyncEnumerator" Version="4.0.2" />
@ -29,6 +29,6 @@
<PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.4.421302"> <PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.4.421302">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.Windows.Compatibility" Version="10.0.3" /> <PackageReference Include="Microsoft.Windows.Compatibility" Version="10.0.3" Condition="$([MSBuild]::IsOsPlatform('Windows'))" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -1,7 +1,6 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Windows.Forms;
namespace MaddoShared; namespace MaddoShared;

View file

@ -73,7 +73,7 @@
<TextBlock Text="Libreria Immagini" FontWeight="Bold" Margin="0,12,0,0" /> <TextBlock Text="Libreria Immagini" FontWeight="Bold" Margin="0,12,0,0" />
<StackPanel Orientation="Horizontal" Margin="0,6,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" /> <RadioButton Content="ImageSharp" IsChecked="{Binding UseImageSharp}" GroupName="Lib" Margin="8,0,0,0" />
</StackPanel> </StackPanel>
</StackPanel> </StackPanel>

View file

@ -5,12 +5,17 @@ using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
#if WINDOWS
using System.Drawing.Text; using System.Drawing.Text;
#endif
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
#if WINDOWS
using System.Windows.Forms; using System.Windows.Forms;
#endif
using System.Windows.Input; using System.Windows.Input;
using AutoMapper; using AutoMapper;
using MaddoShared; using MaddoShared;
@ -258,12 +263,16 @@ namespace ImageCatalog_2
private List<string> LoadAvailableFonts() private List<string> LoadAvailableFonts()
{ {
#if WINDOWS
var fonts = new List<string>(); var fonts = new List<string>();
using (var installedFonts = new InstalledFontCollection()) using (var installedFonts = new InstalledFontCollection())
{ {
fonts.AddRange(installedFonts.Families.Select(f => f.Name)); fonts.AddRange(installedFonts.Families.Select(f => f.Name));
} }
return fonts; return fonts;
#else
return new List<string>();
#endif
} }
private CancellationTokenSource? _mainToken; private CancellationTokenSource? _mainToken;
@ -604,7 +613,12 @@ namespace ImageCatalog_2
} }
// Image library selection (UI radio buttons bind to the boolean helpers) // 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> /// <summary>
/// The selected image processing library. Possible values: "System.Graphics" or "ImageSharp". /// The selected image processing library. Possible values: "System.Graphics" or "ImageSharp".
@ -624,6 +638,7 @@ namespace ImageCatalog_2
NotifyPropertyChanged(); NotifyPropertyChanged();
NotifyPropertyChanged(nameof(UseSystemGraphics)); NotifyPropertyChanged(nameof(UseSystemGraphics));
NotifyPropertyChanged(nameof(UseImageSharp)); NotifyPropertyChanged(nameof(UseImageSharp));
NotifyPropertyChanged(nameof(IsRunningOnWindows));
} }
} }
@ -1506,7 +1521,11 @@ namespace ImageCatalog_2
public event EventHandler<string> LoadSettingsRequested; public event EventHandler<string> LoadSettingsRequested;
public event EventHandler SelectColorRequested; public event EventHandler SelectColorRequested;
// Request that the View shows a message to the user (message, caption, icon) // Request that the View shows a message to the user (message, caption, icon)
#if WINDOWS
public event EventHandler<Tuple<string, string, MessageBoxIcon>> ShowMessageRequested; public event EventHandler<Tuple<string, string, MessageBoxIcon>> ShowMessageRequested;
#else
public event EventHandler<Tuple<string, string, int>> ShowMessageRequested;
#endif
public event EventHandler SelectTransparentColorRequested; public event EventHandler SelectTransparentColorRequested;
private void SelectSourceFolder(object parameter) private void SelectSourceFolder(object parameter)

View file

@ -1,19 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net10.0-windows</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<UseWindowsForms>true</UseWindowsForms>
<UseWPF>true</UseWPF>
<ProduceReferenceAssembly>False</ProduceReferenceAssembly> <ProduceReferenceAssembly>False</ProduceReferenceAssembly>
<!-- Default assembly name for regular builds --> <!-- Default assembly name for regular builds -->
<AssemblyName>ImageCatalog</AssemblyName> <AssemblyName>ImageCatalog</AssemblyName>
<LangVersion>default</LangVersion> <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> <ApplicationIcon>Logo.ico</ApplicationIcon>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup Condition="!$([MSBuild]::IsOsPlatform('Windows'))">
<RuntimeIdentifier>win-x64</RuntimeIdentifier> <TargetFramework>net10.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<!-- Keep MinVer package enabled but do NOT let it overwrite AssemblyVersion/FileVersion used at build-time. <!-- Keep MinVer package enabled but do NOT let it overwrite AssemblyVersion/FileVersion used at build-time.
@ -42,11 +47,11 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="AIFotoONLUS.Core" Version="0.1.1" /> <PackageReference Include="AIFotoONLUS.Core" Version="0.1.1" />
<PackageReference Include="AutoMapper" Version="16.0.0" /> <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.DependencyInjection" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.Logging" 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="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="MinVer" Version="7.0.0" PrivateAssets="all" />
<PackageReference Include="Avalonia" Version="11.3.0" /> <PackageReference Include="Avalonia" Version="11.3.0" />
<PackageReference Include="Avalonia.Desktop" Version="11.3.0" /> <PackageReference Include="Avalonia.Desktop" Version="11.3.0" />

View file

@ -1,4 +1,5 @@
using System; #if WINDOWS
using System;
using System.Diagnostics; using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@ -2242,3 +2243,4 @@ namespace ImageCatalog
} }
} }
} }
#endif

View file

@ -1,4 +1,5 @@
using System; #if WINDOWS
using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
@ -838,3 +839,4 @@ public class PicInfo
NomeImmagine = Nome_Immagine; NomeImmagine = Nome_Immagine;
} }
} }
#endif

View file

@ -1,3 +1,4 @@
#if WINDOWS
using System.Windows; using System.Windows;
using MahApps.Metro.Controls; using MahApps.Metro.Controls;
using ControlzEx.Theming; using ControlzEx.Theming;
@ -378,3 +379,5 @@ namespace ImageCatalog_2
} }
} }
#endif

View file

@ -195,7 +195,7 @@ namespace ImageCatalog_2.Models
// Selected image processing library (e.g., "System.Graphics" or "ImageSharp") // Selected image processing library (e.g., "System.Graphics" or "ImageSharp")
[JsonPropertyName("ImageLibrary")] [JsonPropertyName("ImageLibrary")]
[XmlElement("ImageLibrary")] [XmlElement("ImageLibrary")]
public string ImageLibrary { get; set; } = "System.Graphics"; public string ImageLibrary { get; set; } = "ImageSharp";
// Options // Options
[JsonPropertyName("ForceJpeg")] [JsonPropertyName("ForceJpeg")]

View file

@ -1,4 +1,4 @@
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using ImageCatalog; using ImageCatalog;
using ImageCatalog_2.Services; using ImageCatalog_2.Services;
using MaddoShared; using MaddoShared;
@ -16,6 +16,7 @@ namespace ImageCatalog_2;
static class Program static class Program
{ {
#if WINDOWS
[DllImport("kernel32.dll", SetLastError = true)] [DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AllocConsole(); private static extern bool AllocConsole();
@ -56,6 +57,7 @@ static class Program
Console.SetOut(standardOutput); Console.SetOut(standardOutput);
Console.SetError(standardOutput); Console.SetError(standardOutput);
} }
#endif
public static IServiceProvider ServiceProvider { get; private set; } public static IServiceProvider ServiceProvider { get; private set; }
@ -67,19 +69,20 @@ static class Program
[STAThread] [STAThread]
static void Main(string[] args) 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.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false); System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
AllocConsole(); AllocConsole();
RedirectConsoleOutput(); RedirectConsoleOutput();
#endif
var serviceCollection = new ServiceCollection(); var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection); ConfigureServices(serviceCollection);
ServiceProvider = serviceCollection.BuildServiceProvider(); ServiceProvider = serviceCollection.BuildServiceProvider();
// Resolve WPF MainWindow when available, otherwise fall back to WinForms MainForm
var serviceProvider = ServiceProvider; var serviceProvider = ServiceProvider;
// Determine UI based on command line. Default: WinForms. Use --wpf for WPF, --avalonia for Avalonia. // Determine UI based on command line. Default: WinForms. Use --wpf for WPF, --avalonia for Avalonia.
@ -92,19 +95,16 @@ static class Program
return; return;
} }
#if WINDOWS
if (useWpf) 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(); var wpfApp = new System.Windows.Application();
try 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/Controls.xaml") });
wpfApp.Resources.MergedDictionaries.Add(new System.Windows.ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Fonts.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") }); 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 try
{ {
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(wpfApp, "Light.Blue"); ControlzEx.Theming.ThemeManager.Current.ChangeTheme(wpfApp, "Light.Blue");
@ -116,10 +116,9 @@ static class Program
} }
catch 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; var wpfMain = serviceProvider.GetService(typeof(ImageCatalog_2.MainWindow)) as ImageCatalog_2.MainWindow;
if (wpfMain is not null) if (wpfMain is not null)
{ {
@ -127,26 +126,27 @@ static class Program
return; 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 // Default / fallback to WinForms UI
var mainForm = serviceProvider.GetRequiredService<MainForm>(); var mainForm = serviceProvider.GetRequiredService<MainForm>();
System.Windows.Forms.Application.Run(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) private static void ConfigureServices(ServiceCollection services)
{ {
// Register AutoMapper (new AddAutoMapper overload — provide config and marker types)
services.AddAutoMapper(cfg => { }, typeof(Program)); services.AddAutoMapper(cfg => { }, typeof(Program));
// Register your services here
services.AddTransient<ITestService, TestService>(); services.AddTransient<ITestService, TestService>();
services.AddTransient<ISettingsService, SettingsService>(); services.AddTransient<ISettingsService, SettingsService>();
services.AddTransient<DataModel>(sp => services.AddTransient<DataModel>(sp =>
{ {
// Resolve optional version provider and pass to DataModel
var testService = sp.GetRequiredService<ITestService>(); var testService = sp.GetRequiredService<ITestService>();
var settingsService = sp.GetRequiredService<ISettingsService>(); var settingsService = sp.GetRequiredService<ISettingsService>();
var imageCreation = sp.GetRequiredService<ImageCreationService>(); var imageCreation = sp.GetRequiredService<ImageCreationService>();
@ -159,25 +159,24 @@ static class Program
}); });
services.AddTransient<ImageCreationService>(); services.AddTransient<ImageCreationService>();
#if WINDOWS
services.AddTransient<ImageCreatorGDI>(); services.AddTransient<ImageCreatorGDI>();
#endif
services.AddTransient<ImageCreatorImageSharp>(); services.AddTransient<ImageCreatorImageSharp>();
services.AddTransient<ImageCreatorMapper>(); services.AddTransient<ImageCreatorMapper>();
// Register IImageCreator to be resolved via ImageCreatorMapper which selects concrete implementation at call time
services.AddTransient<IImageCreator>(sp => sp.GetRequiredService<ImageCreatorMapper>()); 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), var userPrefsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"ImageCatalog", "userprefs.xml"); "ImageCatalog", "userprefs.xml");
services.AddSingleton(new ParametriSetup(userPrefsPath)); services.AddSingleton(new ParametriSetup(userPrefsPath));
services.AddSingleton<PicSettings>(); services.AddSingleton<PicSettings>();
// Register your forms #if WINDOWS
services.AddTransient<MainForm>(); services.AddTransient<MainForm>();
// Register WPF MainWindow so it can be resolved with the existing DataModel
services.AddTransient<ImageCatalog_2.MainWindow>(); services.AddTransient<ImageCatalog_2.MainWindow>();
#endif
// Version provider for UI and logging
services.AddSingleton<MaddoShared.IVersionProvider, MaddoShared.VersionProvider>(); services.AddSingleton<MaddoShared.IVersionProvider, MaddoShared.VersionProvider>();
services.AddLogging(configure => services.AddLogging(configure =>

View file

@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Linq; using System.Linq;
@ -6,14 +6,18 @@ using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
#if WINDOWS
using System.Windows.Forms; using System.Windows.Forms;
#endif
namespace ImageCatalog_2 namespace ImageCatalog_2
{ {
public class ViewModelBase : INotifyPropertyChanged public class ViewModelBase : INotifyPropertyChanged
{ {
private readonly SynchronizationContext? _synchronizationContext; private readonly SynchronizationContext? _synchronizationContext;
#if WINDOWS
private Control? _control; private Control? _control;
#endif
protected ViewModelBase() protected ViewModelBase()
{ {
@ -25,10 +29,12 @@ namespace ImageCatalog_2
/// Set a Control to use for thread marshalling in WinForms applications. /// Set a Control to use for thread marshalling in WinForms applications.
/// This is required for proper cross-thread handling with data binding. /// This is required for proper cross-thread handling with data binding.
/// </summary> /// </summary>
#if WINDOWS
public void SetControl(Control control) public void SetControl(Control control)
{ {
_control = control; _control = control;
} }
#endif
public event PropertyChangedEventHandler? PropertyChanged; public event PropertyChangedEventHandler? PropertyChanged;
@ -40,6 +46,7 @@ namespace ImageCatalog_2
if (PropertyChanged == null) if (PropertyChanged == null)
return; return;
#if WINDOWS
// If we have a Control reference (WinForms), use Control.Invoke for proper marshalling // If we have a Control reference (WinForms), use Control.Invoke for proper marshalling
if (_control != null) if (_control != null)
{ {
@ -53,7 +60,9 @@ namespace ImageCatalog_2
} }
} }
// Fallback to SynchronizationContext if available // 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 // We're on a different thread, marshal to the UI thread
_synchronizationContext.Send(_ => _synchronizationContext.Send(_ =>