How to DrawString with character and line spacing using Aspose.Imaging in .Net?

Hello Team,

I want to write “Hello World” using drawstring by setting same space between each character. How can we achieve this? Also, If we write text using drawstring which fall in multiple line then how can we set space between two line?

@siriussynoptek
Please review following code weather it meets your expectation :

public void Run(string outPath)
        {
            using (var image = (RasterImage)Image.Create(new PngOptions { Source = new FileCreateSource(outPath, false), ColorType = PngColorType.TruecolorWithAlpha }, 1000, 1000))
            {
                Color backgroundColor = Color.AntiqueWhite;
                Graphics graphics = new Graphics(image);

                graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
                graphics.Clear(Color.White);
                graphics.PageUnit = GraphicsUnit.Pixel;
                DrawStringExt(graphics, new string[] { "Hello world!" }, new PointF(100, 100), new Font("Arial", 12, FontStyle.Regular), 0, 0);

                DrawStringExt(graphics, new string[] { "Hello world!" }, new PointF(100, 200), new Font("Arial", 12, FontStyle.Regular), 5, 0);

                DrawStringExt(graphics, new string[] { "Hello", "world!" }, new PointF(100, 300), new Font("Arial", 12, FontStyle.Regular), 0, 10);

                image.Save();
            }
        }

        private void DrawStringExt(Graphics graphics, string[] texts, PointF point, Font font, int hotizontalGap, int verticalGap)
        {            
            var stringFormat = new StringFormat(StringFormat.GenericTypographic);
            float y = point.Y;
            foreach (var text in texts)
            {
                var linePlaceHolder = graphics.MeasureString(text, font, SizeF.Empty, stringFormat);
                float x = point.X;
                if (hotizontalGap == 0)
                {
                    graphics.DrawString(text, font, new Aspose.Imaging.Brushes.SolidBrush(Color.Black), x, y, stringFormat);
                }
                else
                {
                    // This will work only for single byte character set
                    for (int i = 0; i < text.Length; i++)
                    {
                        var symbol = text.Substring(i, 1);
                        var symbolPlaceHolder = graphics.MeasureString(symbol, font, SizeF.Empty, stringFormat);
                        graphics.DrawString(symbol, font, new Aspose.Imaging.Brushes.SolidBrush(Color.Black), x, y, stringFormat);

                        x += symbolPlaceHolder.Width + hotizontalGap;
                    }
                }
                y += linePlaceHolder.Height + verticalGap;
            }
        }

Expected results : drawstringext.png (2.4 KB)

Hi…
We have tried to apply code snippet given by you and observed below points.

  • We are able to achieve space between character but it’s look like too much space between two character. I have given hotizontalGap value as 0.02. Please check attached 1.jpg and expected1.jpg image.

  • In our case, if text is reached to object boundary then rest of text is automatically fall in next line but if I applied code given by you then text is not wrapping. Please see attached 2.jpg and expected2_3.jpg image.

  • Our requirement is to display text right align and for that we have set below code. But When I apply code snippet given by you then it’s not working. Please see attached 3.jpg and expected2_3.jpg image for same.

StringFormat otherFormat = new StringFormat()
{
Alignment = StringAlignment.Far,
};

  • Instead of setting space between two line, is it possible to set line height.

Images.zip (41.9 KB)

@siriussynoptek
It’s hard to achieve exact spacing between symbols using current Aspose.Imaging API. We’ve created IMAGINGNET-5012 to enhance the API supporting DrawString with the extra distance between symbols or lines.

The issues you have found earlier (filed as IMAGINGNET-5012) have been fixed in this update. This message was posted using Bugs notification tool by samer.el-khatib4aspose