string.Copy deprecation removes a necessary use case with TextRenderer.MeasureText
See original GitHub issue- .NET Core Version: 3.0 Preview 7
- Have you experienced this same bug with .NET Framework?: No
Problem description: string.Copy was deprecated in .NET Core 3.0 (for good reason), however, I had been using it with TextRenderer.MeasureText, which modifies the string when calling it if the string is too long for the width specified. Without string.Copy, I’m not sure how else you would use this API without string.Copy for now. With the warning showing that this API was removed as strings may be deduplicated in the future, I wasn’t sure about how to properly change this code to be more future-proof, as I’m not sure it can be in its current state.
Minimal repro:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ConsoleApp10
{
class Program
{
static void Main(string[] args)
{
string original = @"C:\New Folder\New Folder\New Folder\New Folder\New Folder\New Folder\text.txt";
string result = GetCompactedString(original, 4);
//Result is now correctly "...\text.txt"
//Expectation is that original is still the very long file path, but it is the same as result
}
public static string GetCompactedString(string stringToCompact, int maxWidth)
{
//This line is necessary to not cause the bug shown below
//string copy= string.Copy(stringToCompact);
string copy = stringToCompact;
var opts = TextFormatFlags.PathEllipsis | TextFormatFlags.ModifyString;
TextRenderer.MeasureText(copy, new Font("Segoe UI", 9), new Size(maxWidth, 0), opts);
// https://connect.microsoft.com/VisualStudio/feedback/details/620330
int nullByteLoc = copy.IndexOf('\0');
if (nullByteLoc != -1)
{
copy = copy.Substring(0, nullByteLoc);
}
return copy;
}
}
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
c# - Why doesn't TextRenderer.MeasureText work properly?
I want to measure the height of the text given a certain width of available canvas. The text that I pass in is...
Read more >TextRenderer.MeasureText Method (System.Windows. ...
Provides the size, in pixels, of the specified text when drawn with the specified font and formatting instructions, using the specified size to...
Read more >Paint | Android Developers
Paint flag that enables the use of bitmap fonts when drawing text. ... The simplest case is when the string contains a single...
Read more >graphics/java/android/graphics/Paint.java
* Paint flag that enables the use of bitmap fonts when drawing text. *. * <p>Disabling this flag will prevent text draw operations...
Read more >Grant Winney - RSSing.com
Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed. Answer by ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Need to validate we don’t allow corrupting the immutability of strings.
#3710 tracks replacing the existing
TextRendererAPI with a safe alternative.