TerribleDog
TerribleDog

Reputation: 1247

Create rectangle for the string that can be resized and center positioned

At first, I used drawString and GraphicsPath.AddString to draw outlined/solid text in the pictureBox. I can change it's font size, style and font-family but I realized that I won't be able to resized/stretch the text since the font size is proportionally distributed to the string. So the solution I was told was this:

I have been advised that in order to scale a Text (from a Draw String), I need to use a rectangle on which the text will depend on. In that way, I can resize the whole text (width, height, both). But I have no idea how to do it.

PS. If there are other ways, you can tell me. Thanks all.

Here's my TextDrawing Method:

public void DrawRects(Font f, string text, Graphics g, RectangleF rect)
    {
        List<RectangleF> list = new List<RectangleF>();
        using (StringFormat format = new StringFormat())
        {
            int i; 
            format.Alignment = StringAlignment.Near;
            format.LineAlignment = StringAlignment.Center;
            format.Trimming = StringTrimming.None;
            format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
            CharacterRange[] ranges = new CharacterRange[text.Length];
            for (i = 0; i < text.Length; i++)
            {
                ranges[i] = new CharacterRange(i, 1);
            }
            format.SetMeasurableCharacterRanges(ranges);
            Region[] regionArray = g.MeasureCharacterRanges(text, f, rect, format);
            for (i = 0; i < regionArray.Length; i++)
            {
                list.Add(regionArray[i].GetBounds(g));
            }
            foreach (RectangleF r in list)
            {
                //g.SmoothingMode = SmoothingMode.AntiAlias;
                //g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
                //g.InterpolationMode = InterpolationMode.High;
                g.DrawRectangle(Pens.LightBlue, Rectangle.Round(r));
            }
            using (GraphicsPath path = new GraphicsPath())
            {
                path.AddString(text, f.FontFamily, Convert.ToInt32(f.Style), g.DpiY * rect.Height/72f, rect.Location, format);
                RectangleF text_rectf = path.GetBounds();
                PointF[] target_pts = {
                        new PointF(rect.Left, rect.Top),
                        new PointF(rect.Right, rect.Top),
                        new PointF(rect.Left, rect.Bottom)};
                g.Transform = new Matrix(text_rectf, target_pts);

                g.FillPath(Brushes.Red, path);
                g.DrawPath(Pens.Red, path);
                g.ResetTransform();
            }
            //g.SmoothingMode = SmoothingMode.AntiAlias;
            //g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
            //g.InterpolationMode = InterpolationMode.High;
            //g.DrawString(text, f, Brushes.Red, rect, format);

        }
    }

And my UI for your reference:

enter image description here

Result I need:

enter image description here

Edit: I changed the code on my text drawing, what I still can't do is to create different rectangles on each letters that is able o be resized using trackbar.

Upvotes: 3

Views: 1208

Answers (1)

Jeremy Thompson
Jeremy Thompson

Reputation: 65544

I had a go and I couldn't work out the rectangles relationship to the letters...

enter image description here


Never the less I propose a more elegant, time-tested and mathematically correct solution.

Alex Fr provided an excellent set of drawing tools in his DrawTools article and this project serves as a basis for Draw Tool Redux.

The original project by Alex Fr was based on a Microsoft C++ MFC sample project which developers may learn from DRAWCLI. The DrawTools C# program reproduces some of DRAWCLI functionality and uses some design decisions from this sample. These days the only way to see it is via WayBack machine link: https://web.archive.org/web/20120814082327/https://www.codeproject.com/Articles/8494/DrawTools

I'd recommend you swap drawing libraries and start off with a really well designed solution. The Draw Tool Redux has most of the functionality I see you need. With the exception of the Rotation Offset which I believe I've seen an example of in Rod Stephens book, here it is on WayBack Machine again: Interactively rotate images by an arbitrary angle in C#.

Upvotes: 1

Related Questions