Skip to content

Commit 07b1482

Browse files
committed
Added System.Drawing.Common reference for Bitmap conversions
1 parent bde6e88 commit 07b1482

28 files changed

+142
-201
lines changed

src/Gsemac.Drawing.Imaging.ImageMagick/Gsemac.Drawing.Imaging.ImageMagick.csproj

+9-8
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,20 @@
3333
<Choose>
3434
<When Condition="$(DefineConstants.Contains('NET45_OR_GREATER')) Or $(DefineConstants.Contains('NETCOREAPP')) Or $(DefineConstants.Contains('NETSTANDARD'))">
3535
<ItemGroup>
36-
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="10.1.0" />
37-
</ItemGroup>
38-
<ItemGroup Condition="$(DefineConstants.Contains('NETFRAMEWORK'))">
39-
<PackageReference Include="Magick.NET.SystemDrawing" Version="4.0.18" />
36+
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="11.1.2" />
37+
<PackageReference Include="Magick.NET.SystemDrawing" Version="5.0.2" />
4038
</ItemGroup>
4139
</When>
4240
<Otherwise>
41+
<!--
42+
Magick.NET-Q16-AnyCPU <= 8.6.1 must be used for compatibility with .NET Framework (later versions target .NET Standard).
43+
The library depends on Magick.NET.Core >= 8.6.1, meaning the latest version of Magick.NET.SystemDrawing we can use is 4.0.12.
44+
Later versions of Magick.NET.SystemDrawing require Magick.NET.Core >= 9.0.0, so trying to use a later version will make VS
45+
complain that we're referencing two different versions of Magick.NET.Core.
46+
-->
4347
<ItemGroup>
4448
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="8.6.1" />
45-
</ItemGroup>
46-
<!-- Magick.NET.SystemDrawing > 4.0.16 is not compatible .NET Framework 4.0 due to the dependency on Magick.NET >= 10.1.0, which only targets .NET Standard. -->
47-
<ItemGroup Condition="$(DefineConstants.Contains('NETFRAMEWORK'))">
48-
<PackageReference Include="Magick.NET.SystemDrawing" Version="4.0.16" />
49+
<PackageReference Include="Magick.NET.SystemDrawing" Version="4.0.12" />
4950
</ItemGroup>
5051
</Otherwise>
5152
</Choose>

src/Gsemac.Drawing.Imaging.NQuant/Gsemac.Drawing.Imaging.NQuant.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
<DebugSymbols>false</DebugSymbols>
2222
</PropertyGroup>
2323

24-
<ItemGroup Condition="$(DefineConstants.Contains('NETFRAMEWORK'))">
24+
<ItemGroup>
2525
<PackageReference Include="nQuant" Version="1.0.3" />
2626
</ItemGroup>
27-
27+
2828
<ItemGroup>
2929
<ProjectReference Include="..\Gsemac.Drawing\Gsemac.Drawing.csproj" />
3030
<ProjectReference Include="..\Gsemac.Reflection.Plugins\Gsemac.Reflection.Plugins.csproj" />

src/Gsemac.Drawing.Imaging.NQuant/NQuantImageOptimizer.cs

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
#if NETFRAMEWORK
2-
3-
using Gsemac.IO;
1+
using Gsemac.IO;
2+
using Gsemac.IO.FileFormats;
43
using Gsemac.Reflection.Plugins;
54
using nQuant;
65
using System.Collections.Generic;
76
using System.Drawing;
87
using System.IO;
9-
using System.Linq;
108

119
namespace Gsemac.Drawing.Imaging {
1210

@@ -57,14 +55,12 @@ public bool Optimize(Stream stream, ImageOptimizationMode optimizationMode) {
5755

5856
private IEnumerable<IFileFormat> GetSupportedImageFormats() {
5957

60-
return new[]{
61-
".png"
62-
}.Select(ext => FileFormatFactory.Default.FromFileExtension(ext));
58+
return new[] {
59+
ImageFormat.Png,
60+
};
6361

6462
}
6563

6664
}
6765

68-
}
69-
70-
#endif
66+
}

src/Gsemac.Drawing.Imaging.WebP/Gsemac.Drawing.Imaging.WebP.csproj

+18-10
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,24 @@
2626
<ProjectReference Include="..\Gsemac.Reflection.Plugins\Gsemac.Reflection.Plugins.csproj" />
2727
</ItemGroup>
2828

29-
<ItemGroup Condition="$(DefineConstants.Contains('NETFRAMEWORK'))">
30-
<Reference Include="System.Drawing" />
31-
<Reference Include="WebPWrapper">
32-
<HintPath>lib\managed\WebPWrapper.dll</HintPath>
33-
</Reference>
34-
</ItemGroup>
29+
<Choose>
30+
<When Condition="$(DefineConstants.Contains('NETFRAMEWORK'))">
31+
<ItemGroup>
32+
<Reference Include="System.Drawing" />
33+
<Reference Include="System.Windows.Forms" />
34+
<Reference Include="WebPWrapper">
35+
<HintPath>lib\managed\WebPWrapper.dll</HintPath>
36+
</Reference>
37+
</ItemGroup>
38+
</When>
39+
<Otherwise>
40+
<ItemGroup>
41+
<PackageReference Include="System.Drawing.Common">
42+
<Version>6.0.0</Version>
43+
</PackageReference>
44+
</ItemGroup>
45+
</Otherwise>
46+
</Choose>
3547

3648
<ItemGroup>
3749
<Content Include="Gsemac.Drawing.Imaging.WebP.targets" PackagePath="build/Gsemac.Drawing.Imaging.WebP.targets" />
@@ -49,10 +61,6 @@
4961
<Content Include="licenses\*" Pack="true" PackagePath="contentFiles\licenses" />
5062
</ItemGroup>
5163

52-
<ItemGroup>
53-
<Reference Include="System.Windows.Forms" />
54-
</ItemGroup>
55-
5664
<ItemGroup>
5765
<Compile Update="Properties\ExceptionMessages.Designer.cs">
5866
<DesignTime>True</DesignTime>

src/Gsemac.Drawing.Imaging.WebP/WebPImage.cs

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
#if NETFRAMEWORK
2-
3-
using Gsemac.IO;
4-
using System;
5-
using System.Collections.Generic;
1+
using Gsemac.IO;
62
using System.Drawing;
7-
using System.Linq;
8-
using System.Text;
93

104
namespace Gsemac.Drawing.Imaging {
115

@@ -58,6 +52,4 @@ protected override void Dispose(bool disposing) {
5852

5953
}
6054

61-
}
62-
63-
#endif
55+
}

src/Gsemac.Drawing.Imaging.WebP/WebPImageCodec.cs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#if NETFRAMEWORK
22

3+
using Gsemac.Drawing.Extensions;
34
using Gsemac.IO;
45
using Gsemac.IO.Extensions;
6+
using Gsemac.IO.FileFormats;
57
using Gsemac.Reflection.Plugins;
68
using System.Collections.Generic;
79
using System.Drawing;
@@ -38,10 +40,7 @@ public IImage Decode(Stream stream, IImageDecoderOptions options) {
3840
using (WebP decoder = new WebP())
3941
using (Image decodedWebPBitmap = DecodeWebPBitmap(decoder, webPData)) {
4042

41-
IImage image = ImageFactory.Default.FromBitmap(decodedWebPBitmap, new BitmapToImageOptions() {
42-
Format = GetSupportedFileFormats().First(),
43-
Codec = this,
44-
});
43+
IImage image = ImageFactory.Default.FromBitmap(decodedWebPBitmap, format: ImageFormat.WebP, codec: this);
4544

4645
IAnimationInfo animationInfo = GetAnimationInfo(decoder, webPData);
4746

@@ -55,9 +54,9 @@ public IImage Decode(Stream stream, IImageDecoderOptions options) {
5554

5655
private IEnumerable<IFileFormat> GetSupportedImageFormats() {
5756

58-
return new[]{
59-
".webp"
60-
}.Select(ext => FileFormatFactory.Default.FromFileExtension(ext));
57+
return new[] {
58+
ImageFormat.WebP,
59+
};
6160

6261
}
6362

src/Gsemac.Drawing/BitmapDataStream.cs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
#if NETFRAMEWORK
2-
3-
using Gsemac.Drawing.Extensions;
4-
using Gsemac.Drawing.Imaging;
5-
using Gsemac.Drawing.Imaging.Extensions;
6-
using System;
1+
using System;
72
using System.Drawing;
83
using System.Drawing.Imaging;
94
using System.IO;
@@ -101,6 +96,4 @@ protected override void Dispose(bool disposing) {
10196

10297
}
10398

104-
}
105-
106-
#endif
99+
}

src/Gsemac.Drawing/Extensions/BitmapExtensions.cs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#if NETFRAMEWORK
2-
3-
using Gsemac.Drawing.Imaging;
1+
using Gsemac.Drawing.Imaging;
42
using Gsemac.IO;
53
using System;
64
using System.Drawing;
@@ -142,6 +140,4 @@ public static Image Resize(this Image bitmap, float? horizontalScale = null, flo
142140

143141
}
144142

145-
}
146-
147-
#endif
143+
}

src/Gsemac.Drawing/Extensions/ImageExtensions.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ public static void Save(this IImage image, string filePath, IFileFormat imageFor
110110

111111
}
112112

113-
#if NETFRAMEWORK
114-
115-
public static Image ToBitmap(this IImage image, IImageToBitmapOptions options) {
113+
public static Image ToBitmap(this IImage image, IBitmapOptions options) {
116114

117115
if (image is null)
118116
throw new ArgumentNullException(nameof(image));
@@ -129,8 +127,6 @@ public static Image ToBitmap(this IImage image, IImageToBitmapOptions options) {
129127

130128
}
131129

132-
#endif
133-
134130
}
135131

136132
}

src/Gsemac.Drawing/Extensions/ImageFactoryExtensions.cs

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
using Gsemac.IO;
1+
using Gsemac.Drawing.Imaging;
2+
using Gsemac.IO;
23
using Gsemac.IO.Extensions;
3-
using System.IO;
4-
using Gsemac.Drawing.Imaging;
54
using System;
6-
7-
#if NETFRAMEWORK
85
using System.Drawing;
9-
#endif
6+
using System.IO;
107

118
namespace Gsemac.Drawing.Extensions {
129

@@ -51,8 +48,6 @@ public static IImage FromStream(this IImageFactory imageFactory, Stream stream)
5148

5249
}
5350

54-
#if NETFRAMEWORK
55-
5651
public static IImage FromBitmap(this IImageFactory imageFactory, Image image) {
5752

5853
if (imageFactory is null)
@@ -61,11 +56,22 @@ public static IImage FromBitmap(this IImageFactory imageFactory, Image image) {
6156
if (image is null)
6257
throw new ArgumentNullException(nameof(image));
6358

64-
return imageFactory.FromBitmap(image, BitmapToImageOptions.Default);
59+
return imageFactory.FromBitmap(image, null, null);
6560

6661
}
62+
public static IImage FromBitmap(this IImageFactory imageFactory, Image image, IFileFormat format, IImageCodec codec) {
6763

68-
#endif
64+
// The "format" and "codec" arguments are allowed to be null, and will be set to defaults by GdiImage's constructor.
65+
66+
if (imageFactory is null)
67+
throw new ArgumentNullException(nameof(imageFactory));
68+
69+
if (image is null)
70+
throw new ArgumentNullException(nameof(image));
71+
72+
return new GdiImage(image, format, codec);
73+
74+
}
6975

7076
}
7177

src/Gsemac.Drawing/Gsemac.Drawing.csproj

+19-5
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,31 @@
2020
<DebugSymbols>false</DebugSymbols>
2121
</PropertyGroup>
2222

23-
<ItemGroup Condition="$(DefineConstants.Contains('NETFRAMEWORK'))">
24-
<Reference Include="System.Drawing" />
25-
<ProjectReference Include="..\Gsemac.Win32\Gsemac.Win32.csproj" />
26-
</ItemGroup>
27-
2823
<ItemGroup>
2924
<ProjectReference Include="..\Gsemac.Core\Gsemac.Core.csproj" />
3025
<ProjectReference Include="..\Gsemac.IO\Gsemac.IO.csproj" />
3126
<ProjectReference Include="..\Gsemac.Reflection.Plugins\Gsemac.Reflection.Plugins.csproj" />
27+
<ProjectReference Include="..\Gsemac.Win32\Gsemac.Win32.csproj" />
3228
</ItemGroup>
3329

30+
<Choose>
31+
<When Condition="$(DefineConstants.Contains('NETFRAMEWORK'))">
32+
<ItemGroup>
33+
<!-- .NET Framework has GDI-related classes in System.Drawing. -->
34+
<Reference Include="System.Drawing" />
35+
</ItemGroup>
36+
</When>
37+
<Otherwise>
38+
<ItemGroup>
39+
<!--
40+
For other targets, GDI-related classes are provided by System.Drawing.Common.
41+
Note that the facilities provided are still only supported on Windows.
42+
-->
43+
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
44+
</ItemGroup>
45+
</Otherwise>
46+
</Choose>
47+
3448
<ItemGroup>
3549
<Compile Update="Properties\ExceptionMessages.Designer.cs">
3650
<DesignTime>True</DesignTime>

src/Gsemac.Drawing/IImage.cs

-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ public interface IImage :
2020

2121
IImage Clone();
2222

23-
#if NETFRAMEWORK
2423
Bitmap ToBitmap();
25-
#endif
2624

2725
}
2826

src/Gsemac.Drawing/IImageFactory.cs

-8
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,13 @@
22
using Gsemac.IO;
33
using System.IO;
44

5-
#if NETFRAMEWORK
6-
using System.Drawing;
7-
#endif
8-
95
namespace Gsemac.Drawing {
106

117
public interface IImageFactory :
128
IHasSupportedFileFormats {
139

1410
IImage FromStream(Stream stream, IImageDecoderOptions options);
1511

16-
#if NETFRAMEWORK
17-
IImage FromBitmap(Image image, IBitmapToImageOptions options);
18-
#endif
19-
2012
}
2113

2214
}

src/Gsemac.Drawing/ImageFactory.cs

-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
using Gsemac.Drawing.Imaging;
22

3-
#if NETFRAMEWORK
4-
using Gsemac.IO;
5-
using System.Drawing;
6-
#endif
7-
83
namespace Gsemac.Drawing {
94

105
public class ImageFactory :

src/Gsemac.Drawing/ImageFactoryBase.cs

-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using Gsemac.IO.Extensions;
44
using System;
55
using System.Collections.Generic;
6-
using System.Drawing;
76
using System.IO;
87

98
namespace Gsemac.Drawing {
@@ -41,22 +40,6 @@ public IImage FromStream(Stream stream, IImageDecoderOptions options) {
4140

4241
}
4342

44-
#if NETFRAMEWORK
45-
46-
public IImage FromBitmap(Image bitmap, IBitmapToImageOptions options) {
47-
48-
if (bitmap is null)
49-
throw new ArgumentNullException(nameof(bitmap));
50-
51-
if (options is null)
52-
throw new ArgumentNullException(nameof(options));
53-
54-
return new GdiImage(bitmap, options.Format, options.Codec);
55-
56-
}
57-
58-
#endif
59-
6043
// Protected members
6144

6245
protected ImageFactoryBase(IImageCodecFactory imageCodecFactory) {

0 commit comments

Comments
 (0)