You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to find a good solution on how to combine text and icons in ImageSharp drawing. I do have a way of doing it currently, but I was wondering if there is a better way of doing it:
This is the code that I currently have:
foreach (var word in words)
{
var wordToRender = word;
var isTab = word == "[tab]";
var isBold = word.StartsWith('|') && word.EndsWith('|');
if (isBold)
{
wordToRender = word.Trim('|');
}
var width = TextMeasurer.MeasureBounds(wordToRender, options);
if (isTab || xPos + width.Width >= initialX + _bounds.Value.Width)
{
xPos = initialX;
yPos += width.Height + 10;
if (isTab)
continue;
}
if (_icons.TryGetValue(word, out var iconPath))
{
using var loadedImage = await Image.LoadAsync(iconPath);
loadedImage.Mutate(i => i.Resize(25, 25));
it.DrawImage(loadedImage, new Point((int)xPos, (int)(yPos - 5)), 1);
xPos += 30;
continue;
}
var path = TextBuilder.GenerateGlyphs(wordToRender, new TextOptions(isBold ? GetBoldFont(_fontSize) : GetFont(_fontSize))
{
Origin = new PointF(xPos, yPos),
HorizontalAlignment = HorizontalAlignment.Left
});
it.Fill(_color, path);
xPos += width.Width + 10;
}
The above code takes things like [image] and then translates it to an actual image.
But I am sure that this could be improved. I now have to manually add spaces between words and account for swapping to the next line, but I feel like this could probably be done somewhere in the package. Maybe through a custom renderer?
If anyone has any ideas, I would love to hear them!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi!
I am trying to find a good solution on how to combine text and icons in ImageSharp drawing. I do have a way of doing it currently, but I was wondering if there is a better way of doing it:
This is the code that I currently have:
The above code takes things like [image] and then translates it to an actual image.
But I am sure that this could be improved. I now have to manually add spaces between words and account for swapping to the next line, but I feel like this could probably be done somewhere in the package. Maybe through a custom renderer?
If anyone has any ideas, I would love to hear them!
Beta Was this translation helpful? Give feedback.
All reactions