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
|
|
@ -1,9 +1,8 @@
|
|||
using System.Drawing;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MaddoShared;
|
||||
|
||||
public interface IImageCreator
|
||||
{
|
||||
Task CreateImageAsync(ImageState imgState, Image logo);
|
||||
Task CreateImageAsync(ImageState imgState, byte[]? logoData);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ public class ImageCreatorImageSharp : IImageCreator
|
|||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task CreateImageAsync(ImageState imgState, System.Drawing.Image logo)
|
||||
public async Task CreateImageAsync(ImageState imgState, byte[]? logoData)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(imgState);
|
||||
|
||||
|
|
@ -74,7 +74,7 @@ public class ImageCreatorImageSharp : IImageCreator
|
|||
var fileNameBig = System.IO.Path.Combine(imgState.DestDir.FullName, imgState.NomeFileBig);
|
||||
|
||||
// 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
|
||||
if (_picSettings.CreaMiniature)
|
||||
|
|
@ -85,7 +85,7 @@ public class ImageCreatorImageSharp : IImageCreator
|
|||
var fileNameSmall = System.IO.Path.Combine(imgState.DestDir.FullName, imgState.NomeFileSmall);
|
||||
|
||||
// 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)
|
||||
|
|
@ -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.
|
||||
// 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,
|
||||
// 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
|
||||
{
|
||||
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))
|
||||
{
|
||||
using var logoStream = File.OpenRead(_picSettings.LogoNomeFile);
|
||||
|
|
@ -301,10 +300,7 @@ public class ImageCreatorImageSharp : IImageCreator
|
|||
}
|
||||
else
|
||||
{
|
||||
// Convert System.Drawing.Image to ImageSharp by saving to PNG in-memory to preserve alpha
|
||||
await using var ms = new MemoryStream();
|
||||
logo.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
|
||||
ms.Seek(0, SeekOrigin.Begin);
|
||||
using var ms = new MemoryStream(logoData);
|
||||
logoImg = await SixLabors.ImageSharp.Image.LoadAsync<Rgba32>(ms).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ namespace MaddoShared;
|
|||
/// <summary>
|
||||
/// Dynamically resolves the concrete IImageCreator implementation at call time
|
||||
/// based on current PicSettings.ImageCreatorProvider.
|
||||
/// On non-Windows platforms only ImageCreatorImageSharp is available.
|
||||
/// </summary>
|
||||
public class ImageCreatorMapper : IImageCreator
|
||||
{
|
||||
|
|
@ -21,29 +22,33 @@ public class ImageCreatorMapper : IImageCreator
|
|||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task CreateImageAsync(ImageState imgState, System.Drawing.Image logo)
|
||||
public Task CreateImageAsync(ImageState imgState, byte[]? logoData)
|
||||
{
|
||||
var provider = (_settings.ImageCreatorProvider ?? "Sharp").Trim();
|
||||
_logger?.LogDebug("Resolving IImageCreator for provider '{Provider}'", provider);
|
||||
|
||||
#if WINDOWS
|
||||
return provider.Equals("ALTERNATE", StringComparison.OrdinalIgnoreCase)
|
||||
? ResolveAndCall<ImageCreatorImageSharp>(imgState, logo)
|
||||
: ResolveAndCall<ImageCreatorGDI>(imgState, logo);
|
||||
? ResolveAndCall<ImageCreatorImageSharp>(imgState, logoData)
|
||||
: 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)
|
||||
{
|
||||
_logger?.LogWarning("Requested image creator {Type} is not registered. Falling back to ImageCreatorGDI.", typeof(T).Name);
|
||||
impl = (IImageCreator)_sp.GetService(typeof(ImageCreatorGDI));
|
||||
_logger?.LogWarning("Requested image creator {Type} is not registered. Falling back to ImageCreatorImageSharp.", typeof(T).Name);
|
||||
impl = (IImageCreator?)_sp.GetService(typeof(ImageCreatorImageSharp));
|
||||
}
|
||||
|
||||
if (impl is null)
|
||||
throw new InvalidOperationException("No IImageCreator implementation is registered.");
|
||||
|
||||
return impl.CreateImageAsync(imgState, logo);
|
||||
return impl.CreateImageAsync(imgState, logoData);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
#if WINDOWS
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Drawing;
|
||||
|
|
@ -17,7 +18,7 @@ namespace MaddoShared;
|
|||
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
|
||||
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
|
||||
{
|
||||
|
|
@ -51,7 +52,7 @@ public class ImageCreatorGDI(PicSettings picSettings, ILogger<ImageCreatorGDI> l
|
|||
|
||||
AddText(g, imgState, imgOutputBig);
|
||||
|
||||
AddLogo(imgOutputBig, logo);
|
||||
AddLogo(imgOutputBig, logoData);
|
||||
|
||||
SavePhoto(imgOutputBig, imgState, thisFormat);
|
||||
}).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
|
||||
if (logo is null) return;
|
||||
// Skip if no logo bytes provided
|
||||
if (logoData is null) return;
|
||||
|
||||
// Load check (use short-circuit &&)
|
||||
if (!(picSettings.LogoAggiungi && File.Exists(picSettings.LogoNomeFile))) return;
|
||||
if (!picSettings.LogoAggiungi) 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.
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
#endif // WINDOWS
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0-windows</TargetFramework>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<OutputType>Library</OutputType>
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
<UseWindowsForms>true</UseWindowsForms>
|
||||
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
|
||||
<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>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AsyncEnumerator" Version="4.0.2" />
|
||||
|
|
@ -29,6 +29,6 @@
|
|||
<PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.4.421302">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Windows.Compatibility" Version="10.0.3" />
|
||||
<PackageReference Include="Microsoft.Windows.Compatibility" Version="10.0.3" Condition="$([MSBuild]::IsOsPlatform('Windows'))" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
using System;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace MaddoShared;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue