markjprice/apps-services-net7

Image Sharp - Chapter 14

Closed this issue · 1 comments

Please check that your issue is not already in the errata aka list of corrections and improvements. Please note that PRs will be ignored because code in the repo must match the print book already published. Please raise an issue instead so I can handle it appropriately. All issues will be entered into the errata once closed and fixed in the next book edition.

Chapter: 14
Page Number: 508-513
Section Title: Implementing a function that works with queues and BLOBs
Problem to fix:
The versions of the Image Sharp nuget package referenced to install seem to be out of date/Image Sharp made changes on their side. When I use version 2.1.3 of SixLabors.ImageSharp with 1.0.0-* or 1.0.0 of SixLabors.ImageSharp.Drawing I get the following errors on build (also removed ".nuget" folder to ensure a clean nuget package install):

/Users/jimcampbell/jimcbell/learning/apps-services-net7/Chapter14/Northwind.AzureFunctions.Services/Northwind.AzureFunctions.Services.csproj : error NU1605: Warning As Error: Detected package downgrade: SixLabors.ImageSharp from 2.1.5 to 2.1.3. Reference the package directly from the project to select a different version. [/Users/jimcampbell/jimcbell/learning/apps-services-net7/Chapter14/Chapter14.sln]
/Users/jimcampbell/jimcbell/learning/apps-services-net7/Chapter14/Northwind.AzureFunctions.Services/Northwind.AzureFunctions.Services.csproj : error NU1605: Northwind.AzureFunctions.Services -> SixLabors.ImageSharp.Drawing 1.0.0 -> SixLabors.ImageSharp (>= 2.1.5) [/Users/jimcampbell/jimcbell/learning/apps-services-net7/Chapter14/Chapter14.sln]
/Users/jimcampbell/jimcbell/learning/apps-services-net7/Chapter14/Northwind.AzureFunctions.Services/Northwind.AzureFunctions.Services.csproj : error NU1605: Northwind.AzureFunctions.Services -> SixLabors.ImageSharp (>= 2.1.3) [/Users/jimcampbell/jimcbell/learning/apps-services-net7/Chapter14/Chapter14.sln]
0 Warning(s)
1 Error(s)

It seems that the version must be updated to 2.1.5 on the ImageSharp.SixLabors. However once you upgrade to 2.1.5 the IPen and IBrush interfaces that are used no longer seem to be available.

S0246: The type or namespace name 'IPen' could not be found (are you missing a using directive or an assembly reference?) [/Users/jimcampbell/jimcbell/learning/apps-services-net7/Chapter14/Northwind.AzureFunctions.Services/Northwind.AzureFunctions.Services.csproj]
/Users/jimcampbell/jimcbell/learning/apps-services-net7/Chapter14/Northwind.AzureFunctions.Services/CheckGeneratorFunction.cs(58,13): error CS0246: The type or namespace name 'IBrush' could not be found (are you missing a using directive or an assembly reference?) [/Users/jimcampbell/jimcbell/learning/apps-services-net7/Chapter14/Northwind.AzureFunctions.Services/Northwind.AzureFunctions.Services.csproj]

This can be fixed by making the types for the objects used for drawing Pen and Brush:
Pen blackPen = Pens.Solid(Color.Black, 2); Pen blackThickPen = Pens.Solid(Color.Black, 8); Pen greenPen = Pens.Solid(Color.Green, 3); Brush redBrush = Brushes.Solid(Color.Red); Brush blueBrush = Brushes.Solid(Color.Blue);

One other thing is that the method signature for IImageProcessorContext no longer takes a TextOptions, it takes a RichTextOptions.
Screenshot 2023-12-24 at 8 50 20 AM

RichTextOptions inherits from TextOptions so this can be handled with an explicit cast:
image.Mutate(x => x.DrawText((RichTextOptions) textOptions,amount, blueBrush, blackPen));
or by declaring a RichTextOptions as the type for the textOptions object instead of a TextOptions:
RichTextOptions textOptions = new(font) { Origin = new PointF(100, 200), WrappingLength = 1000, HorizontalAlignment = HorizontalAlignment.Left };
image.Mutate(x => x.DrawText(textOptions,amount, blueBrush, blackPen));

Suggested solution: In problem.

My image after making these changes:
Screenshot 2023-12-24 at 9 02 37 AM

One more note, I only noticed because I am running this on a Mac instead of my normal Windows. The path separator is directly referenced in the CheckGeneratorFunction.cs with the Windows syntax () which will cause issues on Mac/Unix:

Here are some changes I made that may be useful (code from book is commented for reference):

// load the font file and create a large font FontCollection collection = new(); string pathToFont = System.IO.Path.Combine("fonts", "Caveat", "static", "Caveat-Regular.ttf"); FontFamily family = collection.Add(pathToFont); // FontFamily family = collection.Add(@"fonts\Caveat\static\Caveat-Regular.ttf");

// create blob in the local filesystem // string folder = $@"{System.Environment.CurrentDirectory}\blobs"; string folder = System.IO.Path.Combine(System.Environment.CurrentDirectory, "blobs");

string blobPath = System.IO.Path.Combine(folder,blobName); // string blobPath = $@"{folder}\{blobName}";
Not a major issue but wanted to drop it all here in case it saves someone else some time, thanks!