diff --git a/PdfSharpCore.Test/CreateSimplePDF.cs b/PdfSharpCore.Test/CreateSimplePDF.cs index ce9a4d49..c4090c0b 100644 --- a/PdfSharpCore.Test/CreateSimplePDF.cs +++ b/PdfSharpCore.Test/CreateSimplePDF.cs @@ -88,7 +88,12 @@ public void CreateTestPdfWithImageViaImageSharp() var renderer = XGraphics.FromPdfPage(pageNewRenderer); // Load image for ImageSharp and apply a simple mutation: +#if NET6_0_OR_GREATER + var image = Image.Load(PathHelper.GetInstance().GetAssetPath("lenna.png")); + var format = image.Metadata.DecodedImageFormat; +#else var image = Image.Load(PathHelper.GetInstance().GetAssetPath("lenna.png"), out var format); +#endif image.Mutate(ctx => ctx.Grayscale()); // create XImage from that same ImageSharp image: diff --git a/PdfSharpCore.Test/PdfSharpCore.Test.csproj b/PdfSharpCore.Test/PdfSharpCore.Test.csproj index 58e82ab5..4d05617e 100644 --- a/PdfSharpCore.Test/PdfSharpCore.Test.csproj +++ b/PdfSharpCore.Test/PdfSharpCore.Test.csproj @@ -1,25 +1,26 @@ - + - netcoreapp3.1;net5.0;net6.0;net7.0;net8.0 + netstandard2.0;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0 + latest false false - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/PdfSharpCore/PdfSharpCore.csproj b/PdfSharpCore/PdfSharpCore.csproj index c830a386..58bb473b 100644 --- a/PdfSharpCore/PdfSharpCore.csproj +++ b/PdfSharpCore/PdfSharpCore.csproj @@ -18,6 +18,12 @@ PdfSharpCore is a partial port of PdfSharp.Xamarin for .NET Core Additionally Mi PdfSharp for .NET Core true true + + 1.6.0 + 1.6.0 + 1.6.0 + 1.6.0 + @@ -46,12 +52,22 @@ PdfSharpCore is a partial port of PdfSharp.Xamarin for .NET Core Additionally Mi - - - - - - + + + + + + + + + + + + + + + + diff --git a/PdfSharpCore/Utils/ImageSharpImageSource.cs b/PdfSharpCore/Utils/ImageSharpImageSource.cs index 383863f4..18700690 100644 --- a/PdfSharpCore/Utils/ImageSharpImageSource.cs +++ b/PdfSharpCore/Utils/ImageSharpImageSource.cs @@ -7,40 +7,14 @@ using SixLabors.ImageSharp.Formats.Bmp; using SixLabors.ImageSharp.Formats.Jpeg; using SixLabors.ImageSharp.Formats.Png; +using SixLabors.ImageSharp.Advanced; namespace PdfSharpCore.Utils { - public class ImageSharpImageSource : ImageSource where TPixel : unmanaged, IPixel + public partial class ImageSharpImageSource : ImageSource where TPixel : unmanaged, IPixel { - public static IImageSource FromImageSharpImage(Image image, IImageFormat imgFormat, int? quality = 75) - { - var _path = "*" + Guid.NewGuid().ToString("B"); - return new ImageSharpImageSourceImpl(_path, image, (int)quality, imgFormat is PngFormat); - } - - protected override IImageSource FromBinaryImpl(string name, Func imageSource, int? quality = 75) - { - var image = Image.Load(imageSource.Invoke(), out IImageFormat imgFormat); - return new ImageSharpImageSourceImpl(name, image, (int)quality, imgFormat is PngFormat); - } - - protected override IImageSource FromFileImpl(string path, int? quality = 75) - { - var image = Image.Load(path, out IImageFormat imgFormat); - return new ImageSharpImageSourceImpl(path, image, (int) quality, imgFormat is PngFormat); - } - - protected override IImageSource FromStreamImpl(string name, Func imageStream, int? quality = 75) - { - using (var stream = imageStream.Invoke()) - { - var image = Image.Load(stream, out IImageFormat imgFormat); - return new ImageSharpImageSourceImpl(name, image, (int)quality, imgFormat is PngFormat); - } - } - - private class ImageSharpImageSourceImpl : IImageSource where TPixel2 : unmanaged, IPixel + private partial class ImageSharpImageSourceImpl : IImageSource where TPixel2 : unmanaged, IPixel { private Image Image { get; } private readonly int _quality; @@ -58,11 +32,6 @@ public ImageSharpImageSourceImpl(string name, Image image, int quality, Transparent = isTransparent; } - public void SaveAsJpeg(MemoryStream ms) - { - Image.SaveAsJpeg(ms, new JpegEncoder() { Quality = this._quality }); - } - public void Dispose() { Image.Dispose(); diff --git a/PdfSharpCore/Utils/ImageSharpImageSource_NET6.cs b/PdfSharpCore/Utils/ImageSharpImageSource_NET6.cs new file mode 100644 index 00000000..0cb9ee0e --- /dev/null +++ b/PdfSharpCore/Utils/ImageSharpImageSource_NET6.cs @@ -0,0 +1,68 @@ +#if NET6_0_OR_GREATER + +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.PixelFormats; +using MigraDocCore.DocumentObjectModel.MigraDoc.DocumentObjectModel.Shapes; +using System; +using System.IO; +using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.Formats.Bmp; +using SixLabors.ImageSharp.Formats.Jpeg; +using SixLabors.ImageSharp.Formats.Png; +using SixLabors.ImageSharp.Advanced; + +namespace PdfSharpCore.Utils +{ + public partial class ImageSharpImageSource : ImageSource where TPixel : unmanaged, IPixel + { + + public static IImageSource FromImageSharpImage(Image image, IImageFormat imgFormat, int? quality = 75) + { + var _path = "*" + Guid.NewGuid().ToString("B"); + return new ImageSharpImageSourceImpl(_path, image, (int)quality, imgFormat is PngFormat); + } + + protected override IImageSource FromBinaryImpl(string name, Func imageSource, int? quality = 75) + { + var image = Image.Load(imageSource.Invoke()); + var imgFormat = image.Metadata.DecodedImageFormat; + + return new ImageSharpImageSourceImpl(name, image, (int)quality, imgFormat is PngFormat); + } + + protected override IImageSource FromFileImpl(string path, int? quality = 75) + { + var image = Image.Load(path); + var imgFormat = image.Metadata.DecodedImageFormat; + + return new ImageSharpImageSourceImpl(path, image, (int) quality, imgFormat is PngFormat); + } + + protected override IImageSource FromStreamImpl(string name, Func imageStream, int? quality = 75) + { + using (var stream = imageStream.Invoke()) { + var image = Image.Load(stream); + var imgFormat = image.Metadata.DecodedImageFormat; + + return new ImageSharpImageSourceImpl(name, image, (int)quality, imgFormat is PngFormat); + } + } + + private partial class ImageSharpImageSourceImpl + { + + public void SaveAsJpeg(MemoryStream ms) + { + Image.SaveAsJpeg(ms, new JpegEncoder() { + Quality = this._quality, + ColorType = JpegEncodingColor.YCbCrRatio420, + Interleaved = true, + }); + } + + } + + } +} + +#endif \ No newline at end of file diff --git a/PdfSharpCore/Utils/ImageSharpImageSource_NETSTANDARD.cs b/PdfSharpCore/Utils/ImageSharpImageSource_NETSTANDARD.cs new file mode 100644 index 00000000..fa5367d6 --- /dev/null +++ b/PdfSharpCore/Utils/ImageSharpImageSource_NETSTANDARD.cs @@ -0,0 +1,61 @@ +#if !NET6_0_OR_GREATER + +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.PixelFormats; +using MigraDocCore.DocumentObjectModel.MigraDoc.DocumentObjectModel.Shapes; +using System; +using System.IO; +using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.Formats.Bmp; +using SixLabors.ImageSharp.Formats.Jpeg; +using SixLabors.ImageSharp.Formats.Png; +using SixLabors.ImageSharp.Advanced; + +namespace PdfSharpCore.Utils +{ + public partial class ImageSharpImageSource : ImageSource where TPixel : unmanaged, IPixel + { + + public static IImageSource FromImageSharpImage(Image image, IImageFormat imgFormat, int? quality = 75) + { + var _path = "*" + Guid.NewGuid().ToString("B"); + return new ImageSharpImageSourceImpl(_path, image, (int)quality, imgFormat is PngFormat); + } + + protected override IImageSource FromBinaryImpl(string name, Func imageSource, int? quality = 75) + { + var image = Image.Load(imageSource.Invoke(), out IImageFormat imgFormat); + return new ImageSharpImageSourceImpl(name, image, (int)quality, imgFormat is PngFormat); + } + + protected override IImageSource FromFileImpl(string path, int? quality = 75) + { + var image = Image.Load(path, out IImageFormat imgFormat); + return new ImageSharpImageSourceImpl(path, image, (int) quality, imgFormat is PngFormat); + } + + protected override IImageSource FromStreamImpl(string name, Func imageStream, int? quality = 75) + { + using (var stream = imageStream.Invoke()) + { + var image = Image.Load(stream, out IImageFormat imgFormat); + return new ImageSharpImageSourceImpl(name, image, (int)quality, imgFormat is PngFormat); + } + } + + private partial class ImageSharpImageSourceImpl + { + + public void SaveAsJpeg(MemoryStream ms) + { + Image.SaveAsJpeg(ms, new JpegEncoder() { + Quality = this._quality, + }); + } + + } + + } +} + +#endif diff --git a/SampleApp/SampleApp.csproj b/SampleApp/SampleApp.csproj index 464229b5..b5fdc568 100644 --- a/SampleApp/SampleApp.csproj +++ b/SampleApp/SampleApp.csproj @@ -2,7 +2,8 @@ Exe - netcoreapp3.1;net5.0;net6.0 + netstandard2.0;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0 + latest false enable enable