Thanks to @Watercolor_Games, a solution has been found. While Graphics.DrawString is not consistent with SpriteBatch.DrawString, TextRenderer.DrawText is and should be used instead. Additionally, TextRenderer.MeasureText also provides a more reliable measurement than Graphics.MeasureString.
What follows is the original post…
Hey,
So I’ve noticed that there’s an inconsistency between SpriteBatch.DrawString font rendering and WinForms Graphics.DrawString rendering, specifically in the spacing between the text. Here’s an image showing the issue…
The left side is rendered with MonoGame, the right side is rendered with WinForms. Notice that the text is wider in the WinForms version than it is in the MonoGame version. The WinForms version also has slightly larger line spacing. Each side is 650x600 pixels.
The WinForms version is rendered using a font loaded from the values found in the spritefont file…
public static Win32SpriteFont FromFile(string contentPath, string assetName, Graphics graphics)
{
string file = Path.Combine(contentPath, assetName);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(File.ReadAllText(file));
var fontNameNode = xmlDoc.SelectSingleNode("//XnaContent/Asset/FontName");
var sizeNode = xmlDoc.SelectSingleNode("//XnaContent/Asset/Size");
if (fontNameNode == null || sizeNode == null)
throw new ArgumentException("The specified file does not contain a FontName and/or Size node at the expected XML path.", "file");
Font internalFont = new Font(fontNameNode.InnerXml, float.Parse(sizeNode.InnerXml));
return new Win32SpriteFont(internalFont, graphics) { SpriteFontAsset = Path.GetFileNameWithoutExtension(assetName) };
}
I’m wondering if anybody knows a way to get these two rendering closer to each other. I don’t need it to be super exact, just closer. I would actually prefer to have the WinForms version render the same as the MonoGame version.
The context for what I’m doing is that I’m using the WinForms editor to block out UI with text and image elements. I’ve created simple controls that mimic my MonoGame ones so that I can just drag/arrange them around the editor, then spit out some auto-generated code that I can import into my game to create all the things I need.
I’ve prototyped the concept and it’s working quite well; however, the MonoGame version isn’t rendering the same as my WinForms version, which causes me to set the text incorrectly. I know that I can have MonoGame render inside a Form, but this approach was intended to save time and to utilize the existing functionality of the WinForms editor
Anyway, any help would be greatly appreciated!