0

The question is simple, assuming you have a shape (lets say a rectangle) and a text ("hello" for example), so it'll write the text all accross the borders of the rectangle for as many times as it fits, for example:

hello hello hello hello

hello             hello

hello             hello

hello hello hello hello

In order to do it I assume you'd need to use a graphics variable, I just dont know how to do it.

A code for drawing a string in a bitmap object:

Bitmap tempp = new Bitmap(1, 1);
Graphics g = Graphics.FromImage(tempp);
SizeF w = g.MeasureString("22", new Font("Tahoma", 200));//in order to get the size of the string as a pixel measurement
Bitmap bmp = new Bitmap((int)w.Width+1, (int)w.Height+1);//the bitmap that will contain the text as a picture
RectangleF rectf = new RectangleF(0, 0, (int)w.Width+1, (int)w.Height+1);
g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
StringFormat format = new StringFormat()
{
    Alignment = StringAlignment.Center,
    LineAlignment = StringAlignment.Center
};
g.DrawString("22", new Font("Tahoma", 200), Brushes.Black, rectf, format);
g.Flush();

Thanks in advance.

For second comment:

hello hello hello
hel           llo
hel           llo
hello hello hello
matan justme
  • 371
  • 3
  • 15
  • 1
    Once you have the size of a string-image, calculate the total size of the final rectangle and put several string-image copies upon it, every copy in its own coordinates. Maybe [this link](http://stackoverflow.com/questions/1478022/c-sharp-get-a-controls-position-on-a-form) can help you. – Massimiliano Kraus Apr 16 '17 at 15:44
  • What about the size of the rectangle? What if it is too big for having four "hello"s in a row but too small for having five. Do you then adjust the size of the rectangle? Or do you adjust the spacing between the texts? – Anthony Apr 16 '17 at 15:46
  • @Anthony doing it like so: i'll edit the thread since i can't show a code here. – matan justme Apr 16 '17 at 15:50
  • @Anthony edited my thread, look at the bottom of it. also, the width and height doesn't matter, and the amounts of texts in each row doesn't matter as well. – matan justme Apr 16 '17 at 15:53
  • Hi @matanjustme, I'm just wondering whether I have solved your problem. – Anthony May 11 '17 at 22:05

1 Answers1

1

I hope this is what you need. There isn't much to explain here. The logic is pretty straightforward.

    public string MyString = "Hello"
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        var g = e.Graphics;
        var strFont = new Font("Tahoma", 10);
        var strSizeF = g.MeasureString(MyString, strFont);

        var canvas = new Rectangle
        {
            Height = 200,
            Width = 200,
            Location = new Point(10, 10),
        };

        g.DrawRectangle(new Pen(new SolidBrush(Color.Blue)), canvas);

        var nx = (int)(canvas.Width / strSizeF.Width);
        var ny = (int)(canvas.Height / strSizeF.Height);
        var spacingX = (canvas.Width - nx * strSizeF.Width) / (nx-1);
        var spacingY = (canvas.Height - ny * strSizeF.Height) / (ny-1);

        //draw top row and bottom row
        int i;
        for (i = 0; i < nx; i++)
        {
            g.DrawString(
                MyString,
                strFont,
                Brushes.Black,
                new PointF(canvas.X + i*(strSizeF.Width + spacingX), canvas.Y)
                );
            g.DrawString(
                MyString,
                strFont,
                Brushes.Black,
                new PointF(canvas.X + i * (strSizeF.Width + spacingX), canvas.Y + canvas.Height - strSizeF.Height)
            );
        }

        //divide the string into half
        var isLengthOdd = MyString.Length % 2 != 0;
        var substr1 = MyString.Substring(0, MyString.Length / 2 + (isLengthOdd ? 1 : 0));
        var substr2 = MyString.Substring(MyString.Length / 2, MyString.Length - MyString.Length / 2);
        var substr2SizeF = g.MeasureString(substr2, strFont);
        //draw side rows
        for (i = 1; i < ny - 1; i++)
        {
            g.DrawString(
                substr1,
                strFont,
                Brushes.Black,
                new PointF(canvas.X, canvas.Y + i * (strSizeF.Height + spacingY))
            );
            g.DrawString(
                substr2,
                strFont,
                Brushes.Black,
                new PointF(canvas.X + canvas.Width - substr2SizeF.Width, canvas.Y + i * (strSizeF.Height + spacingY))
            );
        }

    }

Result:

Result

Anthony
  • 3,595
  • 2
  • 29
  • 38
  • Hi, sorry for the late delay, still working on your answer, it gave me some nice directions and helped alot in understanding, so thank you for the answer :). – matan justme Apr 25 '17 at 11:17
  • 1
    I've explored your answer, it didn't really was accurate to what i wanted to do, but it showed me the way of how to work with graphics. Thanks to your answer I've got to what i wanted to get, and therefore I've accepted your answer. – matan justme May 18 '17 at 11:45
  • @matanjustme I appreciate this. If you are willing to, you can answer your own question and accept it. This will help others who find your question and answer useful. :) – Anthony May 18 '17 at 12:02