Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions PdfSharpCore.Test/CreateSimplePDF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
using PdfSharpCore.Test.Helpers;
using PdfSharpCore.Utils;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using Xunit;

namespace PdfSharpCore.Test
Expand Down Expand Up @@ -51,6 +55,32 @@ public void CreateTestPdfWithImage()
ReadStreamAndVerifyPdfHeaderSignature(stream);
}

[Fact]
public void CreateTestPdfWithImageViaImageSharp()
{
using var stream = new MemoryStream();
var document = new PdfDocument();

var pageNewRenderer = document.AddPage();

var renderer = XGraphics.FromPdfPage(pageNewRenderer);

// Load image for ImageSharp and apply a simple mutation:
var image = Image.Load<Rgb24>(PathHelper.GetInstance().GetAssetPath("lenna.png"), out var format);
image.Mutate(ctx => ctx.Grayscale());

// create XImage from that same ImageSharp image:
var source = ImageSharpImageSource<Rgb24>.FromImageSharpImage(image, format);
var img = XImage.FromImageSource(source);

renderer.DrawImage(img, new XPoint(0, 0));

document.Save(stream);
stream.Position = 0;
Assert.True(stream.Length > 1);
ReadStreamAndVerifyPdfHeaderSignature(stream);
}

private void SaveDocument(PdfDocument document, string name)
{
var outFilePath = GetOutFilePath(name);
Expand Down
2 changes: 1 addition & 1 deletion PdfSharpCore.Test/PdfSharpCore.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
7 changes: 7 additions & 0 deletions PdfSharpCore/Utils/ImageSharpImageSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ namespace PdfSharpCore.Utils
{
public class ImageSharpImageSource<TPixel> : ImageSource where TPixel : unmanaged, IPixel<TPixel>
{

public static IImageSource FromImageSharpImage(Image<TPixel> image, IImageFormat imgFormat, int? quality = 75)
{
var _path = "*" + Guid.NewGuid().ToString("B");
return new ImageSharpImageSourceImpl<TPixel>(_path, image, (int)quality, imgFormat is PngFormat);
}

protected override IImageSource FromBinaryImpl(string name, Func<byte[]> imageSource, int? quality = 75)
{
var image = Image.Load<TPixel>(imageSource.Invoke(), out IImageFormat imgFormat);
Expand Down