1

Using Six Labors ImageSharp 1.0.4 for .Net core, how can I draw a line between two points? My code below combines three images into the GridImage and then I want to draw a line on the new image.

When I call imageContext.DrawLines(linePen, points) I am getting the error:

The type IImageProcessingContext<> is defined in a reference that is not referenced to assembly Sixlabors.ImageSharp.

However, I am referencing SixLabors in the using statement.

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System;
using System.IO;

public void CombineImagesDrawLine() {
    using (Image<Rgba32> img1 = Image.Load<Rgba32>("gridTemplate.png"))
    using (Image<Rgba32> img2 = Image.Load<Rgba32>("circleOne.png"))
    using (Image<Rgba32> img3 = Image.Load<Rgba32>("circleTwo.png"))

    using (Image<Rgba32> GridImage = new Image<Rgba32>(1024, 576)) // create output image of the correct dimensions
    {
        // take img1 and img2 and draw them onto the image
        GridImage.Mutate(o => o
            .DrawImage(img1, new Point(0, 0), 1f)
            .DrawImage(img2, new Point(100, 100), 1f) 
        );

        // Add img3
        GridImage.Mutate(o => o
            .DrawImage(img3, new Point(200, 250), 1f)
        );

        GridImage.Mutate(imageContext =>
        {
            var points = new SixLabors.Primitives.PointF[2];
            points[0] = new SixLabors.Primitives.PointF(
                x: (float)(89),
                y: (float)(118));
            points[1] = new SixLabors.Primitives.PointF(
                x: (float)(901),
                y: (float)(379));

            var lineColor = SixLabors.ImageSharp.Color.FromRgb(
                r: (byte)124,
                g: (byte)190,
                b: (byte)75);
            float lineWidth = 5;

            var linePen = new Pen(lineColor, lineWidth);                     

            imageContext.DrawLines(linePen, points); // !!This is where my code fails
     }
}
Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
launchCode
  • 23
  • 5
  • Does this answer your question? [The type is defined in an assembly that is not referenced, how to find the cause?](https://stackoverflow.com/questions/20660999/the-type-is-defined-in-an-assembly-that-is-not-referenced-how-to-find-the-cause) – Metro Smurf Dec 15 '21 at 16:06
  • I saw that post yesterday during my troubleshooting and it didn't help. I am confused on this one, but I feel like I need to create a class reference. Here is the documentation for this class and method https://docs.sixlabors.com/api/ImageSharp.Drawing/SixLabors.ImageSharp.Drawing.Processing.DrawLineExtensions.html – launchCode Dec 15 '21 at 17:36

1 Answers1

1

The error is stating Sixlabors.ImageSharp is referencing another assembly that your project is missing.

The Github samples for Sixlabors shows these references in the .csproj:

<ItemGroup>
  <PackageReference Include="SixLabors.Fonts" Version="1.0.0-beta15" />
  <PackageReference Include="SixLabors.ImageSharp" Version="1.0.3" />
</ItemGroup>

And looking at the dependencies in Nuget for SixLabors.ImageSharp.Drawing:

.NETCoreApp 2.1
SixLabors.Fonts (>= 1.0.0-beta15)
SixLabors.ImageSharp (>= 1.0.3)

.NETCoreApp 3.1
SixLabors.Fonts (>= 1.0.0-beta15)
SixLabors.ImageSharp (>= 1.0.3)

.NETFramework 4.7.2
SixLabors.Fonts (>= 1.0.0-beta15)
SixLabors.ImageSharp (>= 1.0.3)

.NETStandard 1.3
NETStandard.Library (>= 1.6.1)
SixLabors.Fonts (>= 1.0.0-beta15)
SixLabors.ImageSharp (>= 1.0.3)

.NETStandard 2.0
SixLabors.Fonts (>= 1.0.0-beta15)
SixLabors.ImageSharp (>= 1.0.3)

.NETStandard 2.1
SixLabors.Fonts (>= 1.0.0-beta15)
SixLabors.ImageSharp (>= 1.0.3)

If you're not using Nuget to pull the package down, then try removing the current reference and then re-referencing with the Nuget package.

Otherwise, remove the current SixLabors reference and then grab the latest Release from SixLabors, and add to your project.

Finally, be sure to read the SixLabors installation documentation.

James South
  • 10,147
  • 4
  • 59
  • 115
Metro Smurf
  • 37,266
  • 20
  • 108
  • 140
  • I have tried multiple times to get the package references correct, but it isn't cooperating after going through the documentation multiple times. I have gone to another solution for imaging. GrapeCity's GcDocuments for Imaging. There is a cost, but works great! – launchCode Dec 17 '21 at 18:22
  • 1
    @launchCode have you tried creating the most basic of projects following their guidance / walk throughs? If you can get that to run, then you should be able to figure out why your actual project is failing. – Metro Smurf Dec 17 '21 at 19:44
  • I've just had a similar problem - needed to tick "Include prerelease" in the Nuget Package Manager to see the beta version of `SixLabors.ImageSharp.Drawing`. After that the [blog I was following](https://swharden.com/csdv/platforms/imagesharp/) worked perfectly. – mft25 May 08 '23 at 21:37