Graphics.MeasureString() incorrectly calculates charactersFitted when executed on Linux and the app targets .NET 5.0
Description
Works on .NET Core 2.1 and 3.1, doesn't when upgrading to .NET 5.0
The call to MeasureString returns unexpected results for fittedCharacters out parameter, when the measured string contains \n and spaces. In the code below, the first measured string is "Lorem ipsum", and the charactersFitted is set to 11, which is correct. The second iteration measures "\n \n" and the expected result is 1, but the charactersFitted is again 11. The same result is kept for the rest of the text chunks. The issue appears only when the code is ran on Linux - either in Docker container or in a regular OS. The issue is not reproducible when the project's TargetFramework is changed to .NET Core 2.1+. Here is the code that demonstrates the bug:
static void Main(string[] args)
{
foreach (var chunk in new[] { "Lorem ipsum", "\n \n", "\n ", "\n" })
{
using (var image = new Bitmap(100, 100))
using (var graphics = Graphics.FromImage(image))
{
int charactersFitted;
int linesFilled;
var size = graphics.MeasureString(
chunk,
SystemFonts.DefaultFont,
new SizeF(100, 100),
StringFormat.GenericTypographic,
out charactersFitted,
out linesFilled
);
Debug.WriteLine($"{chunk}: {charactersFitted}");
}
}
}
Here's the dockerfile I use:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/runtime:5.0-buster-slim AS base
RUN apt-get update \
&& apt-get install -y --allow-unauthenticated \
libc6-dev \
libgdiplus \
libx11-dev \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["GraphicsMeasurementBug/GraphicsMeasurementBug.csproj", "GraphicsMeasurementBug/"]
RUN dotnet restore "GraphicsMeasurementBug/GraphicsMeasurementBug.csproj"
COPY . .
WORKDIR "/src/GraphicsMeasurementBug"
RUN dotnet build "GraphicsMeasurementBug.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "GraphicsMeasurementBug.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "GraphicsMeasurementBug.dll"]
Configuration
Tested on .NET 5.0, .NET Core 2.1, .NET Core 3.1, Windows and Ubuntu 18.04.
Regression?
The same code works when the app targets .NET Core 2.1 or .NET Core 3.1. Works also on Windows.
Other information
Seems that the out parameter charactersFitted does not get reset to default(int) in some circumstances - i.e. when the measured text contains \n characters.
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.
Tagging subscribers to this area: @safern, @tannergooding See info in area-owners.md if you want to be subscribed.
Issue Details
Description
The call to MeasureString returns unexpected results for fittedCharacters out parameter, when the measured string contains \n and spaces. In the code below, the first measured string is "Lorem ipsum", and the charactersFitted is set to 11, which is correct. The second iteration measures "\n \n" and the expected result is 1, but the charactersFitted is again 11. The same result is kept for the rest of the text chunks. The issue appears only when the code is ran on Linux - either in Docker container or in a regular OS. The issue is not reproducible when the project's TargetFramework is changed to .NET Core 2.1+. Here is the code that demonstrates the bug:
static void Main(string[] args)
{
foreach (var chunk in new[] { "Lorem ipsum", "\n \n", "\n ", "\n" })
{
using (var image = new Bitmap(100, 100))
using (var graphics = Graphics.FromImage(image))
{
int charactersFitted;
int linesFilled;
var size = graphics.MeasureString(
chunk,
SystemFonts.DefaultFont,
new SizeF(100, 100),
StringFormat.GenericTypographic,
out charactersFitted,
out linesFilled
);
Debug.WriteLine($"{chunk}: {charactersFitted}");
}
}
}
Here's the dockerfile I use:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/runtime:5.0-buster-slim AS base
RUN apt-get update \
&& apt-get install -y --allow-unauthenticated \
libc6-dev \
libgdiplus \
libx11-dev \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["GraphicsMeasurementBug/GraphicsMeasurementBug.csproj", "GraphicsMeasurementBug/"]
RUN dotnet restore "GraphicsMeasurementBug/GraphicsMeasurementBug.csproj"
COPY . .
WORKDIR "/src/GraphicsMeasurementBug"
RUN dotnet build "GraphicsMeasurementBug.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "GraphicsMeasurementBug.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "GraphicsMeasurementBug.dll"]
Configuration
Tested on .NET 5.0, .NET Core 2.1, .NET Core 3.1, Windows and Ubuntu 18.04.
Regression?
The same code works when the app targets .NET Core 2.1 or .NET Core 3.1. Works also on Windows.
Other information
Seems that the out parameter charactersFitted does not get reset to default(int) in some circumstances - i.e. when the measured text contains \n characters.
| Author: | ighristov |
|---|---|
| Assignees: | - |
| Labels: |
|
| Milestone: | - |
Thanks @ighristov this a dupe of: https://github.com/dotnet/runtime/issues/25139 and https://github.com/dotnet/runtime/issues/25124
The underlying issues are on libgdiplus which we have the issues for it in mono: https://github.com/mono/libgdiplus/search?q=MeasureString&type=issues
At the moment System.Drawing.Common support in Linux is yet unclear and we recommend using other libraries like SkiaSharp or ImageSharp if possible.
See these remarks for more info: https://docs.microsoft.com/en-us/dotnet/api/system.drawing?view=dotnet-plat-ext-5.0#remarks and please let me know if you have any questions I can help answer.
Hello and thanks for the fast response.
I'm aware of the numerous issues with libgdiplus on Linux, mentioned in #25139 and #25124 but this one seems to be introduced with .NET 5. The same code when used in a .NET Core 2.1 or .NET Core 3.1 on the same Linux distro works as expected (although providing not the same results as on Windows), but when targeting .NET 5 framework, the behavior changes. The strangest thing to me is not that MeasureString counts the characters incorrectly - I would expect that to some extent - but that it doesn't even bother to assign a default value to charactersFitted parameter. Just looking at the code snippet provided above, one would expect to get values like 11, 1, 1, 0 in the console output, but definitely not 11, 11, 11, 11, as it is now.
My workaround for the time being is to explicitly set a value to charactersFitted declaration, i.e. int charactersFitted = 0;, but that just looks strange and also causes unnecessary value assignment (IDE0059) rule violation.
All best, Ivan Hristov
Thanks, @ighristov for the detailed response. Given that this seems to be a regression from 3.1 to 5.0, I will re-open and keep tracked to take a look at.
Given that we are no longer supporting System.Drawing.Common on non windows going forward: https://github.com/dotnet/designs/blob/main/accepted/2021/system-drawing-win-only/system-drawing-win-only.md
I'm going to close this issue. Please let me know if you have further questions.