none
Graphic buffer understanding RRS feed

  • Question

  • I cant seem to figure out why there is a good amount of space after the text that I draw onto a picturebox. Basically I set up the size of the picturebox to be equal to the largest width and height and resize my buffer before actually drawing to the object.

    Following is my code snippet:

        Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
            'place a panel on the form and inside of it place a picturebox
            'and place the following code inside of a button control
            Dim backbuffer As New Bitmap(Panel1.Width, Panel1.Height)
            Dim t As String = "This is a test12345678901234567890123456789k"
            Dim x, y As Single
            Using g As Graphics = Graphics.FromImage(backbuffer),
                f1 = New Font("calabri", 24, FontStyle.Regular),
                fmt As StringFormat = CType(StringFormat.GenericTypographic.Clone, StringFormat)
                fmt.FormatFlags = fmt.FormatFlags Or StringFormatFlags.MeasureTrailingSpaces
                Dim stringsize As SizeF
                stringsize = g.MeasureString(t, f1, New Point(CInt(0), CInt(0)), fmt)
                x = stringsize.Width
                y = stringsize.Height
            End Using
            If x < Panel1.Width Then
                x = Panel1.Width
            End If
            If y < Panel1.Height Then
                y = Panel1.Height
            End If
            backbuffer.Dispose()
            backbuffer = New Bitmap(CInt(x), CInt(y))
    
            Using g As Graphics = Graphics.FromImage(backbuffer)
                g.Clear(Color.White) 'paint the buffer white
                g.DrawString(t, New Font("Calibri", 24, FontStyle.Regular), New SolidBrush(Color.Black), New Point)
            End Using
            PictureBox1.Image = backbuffer
            PictureBox1.Size = backbuffer.Size
        End Sub
    

    Thanks for any help

    Les

    Wednesday, March 25, 2020 10:25 PM

Answers

  • @Les2011

    The white space at the end that you describe in your first post is almost certainly caused by a typo in your code.

    When you draw your text, you specify the font as:
          New Font("Calibri", 24, FontStyle.Regular)

    However, when you measure the size of the text, you specify the font as:
          f1 = New Font("calabri", 24, FontStyle.Regular)

    Different fonts usually have different metrics. It's unlikely you have a font named "calabri" on your system, so MS Sans Serif will be substituted by default, and that will definitely have different metrics to the Calibri Font that you ultimately use to render the text.




    • Proposed as answer by Cor Ligthert Friday, March 27, 2020 5:18 PM
    • Marked as answer by Les2011 Saturday, March 28, 2020 5:12 AM
    Friday, March 27, 2020 5:02 PM
  • About the stop, are you sure you have nowhere something as End or Application.Exit in your program. 

    That should be done by the application framework which is done by a hidden module and therefore only stop with close.

    Did you see the reply from S P C, seems to me a great catch I did not notice it either. 


    Success
    Cor

    • Marked as answer by Les2011 Saturday, March 28, 2020 5:15 AM
    Friday, March 27, 2020 5:21 PM

All replies

  • Hi Les

    Not sure what you are trying to acheive. Would you explain further.

    Are you trying to size the text to fit the PictureBox, or, fit the PictureBox to the text? You seem to be drawing the string at a fixed point size and so it would always be the same, therefor I would imagine then that you want the PictureBox to resize to fit the textt - but then again, I am likely to be way off the mark with my interpretation.

    Here is a doodle which attempts to resize the text to whatever the PictureBox is resized to (the Button redraws text to fit). Note the uswe of ScaleTransform which *might( be useful.

    ' Form1 with Button1 and
    ' PictireBox1 Docked in
    ' Panel1 (anchored left, bottom, right)
    
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Button1.PerformClick()
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim backbuffer As New Bitmap(Panel1.Width, Panel1.Height)
        Dim t As String = "This is a test12345678901234567890123456789k  "
        '  Dim x, y As Single
        '   Using g As Graphics = Graphics.FromImage(backbuffer)
    
        Dim f1 As New Font("Calabri", 24, FontStyle.Regular)
    
        '  Dim fmt As StringFormat = CType(StringFormat.GenericTypographic.Clone, StringFormat)
    
        '  fmt.FormatFlags = fmt.FormatFlags Or StringFormatFlags.MeasureTrailingSpaces
        '  Dim stringsize As SizeF = g.MeasureString(t, f1, New Point(CInt(0), CInt(0)), fmt)
        '  x = stringsize.Width
        '  y = stringsize.Height
        'End Using
    
        'If x < Panel1.Width Then
        '  x = Panel1.Width
        'End If
        'If y < Panel1.Height Then
        '  y = Panel1.Height
        'End If
        'backbuffer.Dispose()
        'backbuffer = New Bitmap(CInt(x), CInt(y))
    
        Dim tr As Size = TextRenderer.MeasureText(t, f1)
        Dim pw As Double = PictureBox1.Width / tr.Width
        Dim pw2 As Double = PictureBox1.Height / tr.Height
    
        Using g As Graphics = Graphics.FromImage(backbuffer)
          g.ScaleTransform(CSng(pw), CSng(pw2))
          g.Clear(Color.Khaki)
          g.DrawString(t, f1, New SolidBrush(Color.Black), New Point)
        End Using
        PictureBox1.Image = backbuffer
        PictureBox1.Size = backbuffer.Size
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, March 26, 2020 12:05 AM
    Thursday, March 26, 2020 12:03 AM
  • Hi Leshay,

    Well I am just wondering if you tried my code why is there a space after the text when in fact it should end up the edge of the text box.  Let me see if I can generate a picture of what I am trying to say.  After the k the blank space that exists seems to be there no matter how long my text string goes.  I am wondering if there is some kind of automatic space that gets placed there when a picturebox is inside of a panel?? Just a quick thought.  If I know the width of my text string and set that equal to my picturebox width then why the extra amount of blank space.  I hope I am explaining it better for you.  My understanding is once I set the picturebox width = to the length of my text string how can this blank space exist.  I am far from an expert on this subject but over the years you and TTT and karen have always helped me with my questions.

    Thanks,

    Les

    Thursday, March 26, 2020 12:36 AM
  • Hi

    Funnily enogh, I had the opposite effect when I tried your code. The 'k' at the end was 'off the edge' and not visible. With the altered code, I actually had to add 2 space characters at the end of the string to get everything inside the PictureBox.

    OK, what all this amounts to in my opinion, is that your screen resolution/scale is different to mine - I have mine set at 225% on a high res screen (I need that due top my old eyes not being up to the task anymore). Also, in my experience, the 2 ways to Measure a string are very pernickety when your screen is not a t 100% scale.

    You can see some of the 'weitdness' with the code I posted if you run it and resize - Button click - resie - button click repeatedly.


    Regards Les, Livingston, Scotland

    Thursday, March 26, 2020 12:49 AM
  • Hi Leshay,

    I am assuming when you talk screen resolution you mean the magnification of contents on your screen and you are in fact correct mine is at 100%.  But I still don't see how that is an issue.  If I measure the text screen to be "X" and I use the following logic:

    If "X" <= panel1.height then

    picturebox1.width=panel.width

    else

    picturebox1.width=X

    endif

    No matter what your screen magnification is set to should not make a difference.  I will play with your code suggestion and see what happens.

    Thx

    Les

    Thursday, March 26, 2020 3:55 AM
  • Hi Leshay,

    Ok I ran your code and some differences I see.  First of all I think in your code and the way it is running does not produce scrollbars as soon as the picturebox.width exceeds the width of the panel which is what I had done.  When I ran preliminary tests on this I placed a picturebox into a panel and using code grew the width of the pictureebox slowly and as soon as it was larger then the panel it produced srollbar whereas I think with your code it is scaling whats inside the picturebox and doing that will never exceed the width of the panel.  BUt what is also strange when I ran your code is the text string is so squished together its almost not even readable.

    Just cant figure this darn thing out.  And you know Leshay its not that what I have written doesn't work but logically the way the spacing happens defies what I had coded and just need to understand what is going on.

    I thank you for your attention here and if you have any other ideas please share them.

    Les

    Thursday, March 26, 2020 4:12 AM
  • Listen I was playing with my code snippet I sent you here and here is so far what I can tell:

    1) The space at the end of the text string varies as you change the font size

    2) The space at the end of the text string appears to be sensitive to the font name you pick

    ie In my code I used Calibri and I get that big space, however when I change it to courier new not only is there NO space after the string but the tail end of the string is chopped off.

    There must be something about using the stringsize method vs some other method perhaps the rendering method or whatever else is out there.  Have to do more research

    Talk soon,

    Les

    Thursday, March 26, 2020 4:43 AM
  • From the Microsoft documentation.

    Remarks

    The MeasureString method is designed for use with individual strings and includes a small amount of extra space before and after the string to allow for overhanging glyphs. Also, the DrawString method adjusts glyph points to optimize display quality and might display a string narrower than reported by MeasureString. To obtain metrics suitable for adjacent strings in layout (for example, when implementing formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that takes a StringFormat, and pass GenericTypographic. Also, ensure the TextRenderingHint for the Graphics is AntiAlias.


    Success
    Cor

    Thursday, March 26, 2020 5:18 AM
  • Hi Cor,

    Yeah OK <S>.  I must say that I finally thought I was understanding this stuff and now you throw me all this other NEW stuff <S>.  As always Cor I am teasing and I appreciate the help but let me ask you are these changes your mentioning difficult to implement?  to be quite honest I have never worked with the things you mentioned.  I will try to read up on these new areas and want to thank you for your help.

    ps Cor I get the small extra space you are talking about before and after but when you look at my message that's a very big difference then for a small glyph extra? No?

    Les

    Thursday, March 26, 2020 6:46 AM
  • Les,

    I thought you did know how to debug. I did it with your code. Setting y and x  to  the panel width and height makes that your bitmap has always the size of your the panel as it is at that moment. (And than set with those panel values your bitmap)

    If you change that correct then it looks complete different. Although there stays a small white part behind the shown string.

         f1 = New Font("calabri", 24, FontStyle.Regular),
                fmt As StringFormat = CType(StringFormat.GenericTypographic.Clone, StringFormat)
                fmt.FormatFlags = fmt.FormatFlags Or StringFormatFlags.MeasureTrailingSpaces
                Dim stringsize As SizeF
                stringsize = g.MeasureString(t, f1, New Point(CInt(0), CInt(0)), fmt)
                x = CInt(stringsize.Width)
                y = CInt(stringsize.Height)
            End Using
            If x < Panel1.Width Then
                Panel1.Width = x
            End If
            If y < Panel1.Height Then
                Panel1.Height = y
            End If

    Be aware that disposing an object that has been made in the method, makes not any sense as it goes anyway out of scope at the end of the method (as it is not longer in use) and therefore only cost time.

    I did not check it any further to get the slight space at the end removed. 

      

    Success
    Cor



    Thursday, March 26, 2020 12:49 PM
  • Hi Cor,

    First I do know how to debug I simply was humorously referring to your ideas of for which I knew nothing about <S> but good references for thought

      MeasureCharacterRanges

    GenericTypographic

     TextRenderingHint

      Graphics is AntiAlias

    I ran your code snippet but what it does it adjusts the size of the panel which I do not want to do.  The whole idea about putting the picturebox inside the panel is as the picturebox in either x or y direction exceeds that of the panel then the scrollbars will pop up and you still can see the entire text string.  Your suggestion is changing the panelbox and I can not really allow that to happen.

    I thank you for your attention to my issue and perhaps you did not realize what I was trying to accomplish. I want to keep the size of the panelbox and when necessary grow the picturebox inside so when the picturebox is smaller then the panelbox make it the size of the panel box and when it is larger make the picturebox the larger size.

    Hope this explains it better,

    Les

    Thursday, March 26, 2020 6:05 PM

  • I thank you for your attention to my issue and perhaps you did not realize what I was trying to accomplish. I want to keep the size of the panelbox and when necessary grow the picturebox inside so when the picturebox is smaller then the panelbox make it the size of the panel box and when it is larger make the picturebox the larger size.

    Hope this explains it better,

    Les

    Then why do you think there is good amount of space drawn.  

    You draw a string on a bitmap which is wider than your bitmap which has a white background. (We cannot seen how your design looks). 

    Measurestring is never exact, but that I did already show in one of the previous replies. 

    If you try it with the code I gave you can see that it is depending from your screen setting about 2 W width. 


    Success
    Cor


    Thursday, March 26, 2020 6:29 PM
  • Hi Cor,

    When you run the code with your modifications doesn't that adjust the size of the panel?  If it does not then maybe I made a mistake implementing your code.  Let me try it again I could have screwed up <S>.

    Thanks Cor I will get to the bottom of this

    Les

    Thursday, March 26, 2020 8:51 PM

  • I ran your code snippet but what it does it adjusts the size of the panel which I do not want to do.  The whole idea about putting the picturebox inside the panel is as the picturebox in either x or y direction exceeds that of the panel then the scrollbars will pop up and you still can see the entire text string.  Your suggestion is changing the panelbox and I can not really allow that to happen.

    I thank you for your attention to my issue and perhaps you did not realize what I was trying to accomplish. I want to keep the size of the panelbox and when necessary grow the picturebox inside so when the picturebox is smaller then the panelbox make it the size of the panel box and when it is larger make the picturebox the larger size.

    Hope this explains it better,

    Les

    Hi

    You seem to be setting impossible targets.

    If the PictureBox is 'smaller then the panelbox make it the size of the panel box', but without changing the font, that would most likely be impossible as the PictureBox is made to fit the string.

    To explain what I mean:

    The Panel is fixed size of (say) 300 x 60 and cannot be changed.

    You feed it with a string which makes the PictureBox exceed both the width and the height of the Panel - what do you want to happen.

    Reduce the font until the PictureBox height fits in the Panel, with the width extending off to the right - scroll bar visible.

    Reduce the font until the wifth fits in the Panel (I know, this is NOT what you want.As the PictureBox fimensions are determined by the string/font, anf the Panel is fixed size, you HAVE to change the font to make the PictureBox EITHER same width OR same height but not both (you saw the result of that with one of the code samples Iposted)

    However, here is some more test example code. This example resizes the Panel height to suit the font size height, but not the width. If the string is longer than fits the Panel, the scroll bar shows and string can be srolled into view. It uses randomly chosen strings of different lengths and random font sizes. The point being to show the results of combining string length with font size and allowing one dimension of the Panel to be adjusted. If the Panel remained fixed size, then both the horizontal and verticle scroll bars would be needed to see the two dimensions of the string.

    Each Button click produces a new test string/font. The label shows the final font size.

    ' blank Form1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim p As New Panel
      Dim lab, lab2 As New Label
      Dim WithEvents Button1 As New Button
      Dim r As New Random
      Dim TestStrings As New List(Of String)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With TestStrings
          .Add("This is a test 12345678901234567890123456789k")
          .Add("Test String 1")
          .Add("A longer Test String 2")
          .Add("A slightly longer Test String 3")
          .Add("A yet longer Test String 3 to test again")
          .Add("Shorter")
          .Add("Short")
          .Add("ABC")
        End With
        With p
          .Location = New Point(10, 10)
          .Size = New Size(300, 60)
          .BorderStyle = BorderStyle.FixedSingle
          .AutoScroll = True
        End With
        With Button1
          .Text = "GO"
          .Location = New Point(p.Right + 8, p.Top + 8)
        End With
        With lab
          .Location = New Point(0, 0)
          .AutoSize = True
        End With
        With lab2
          .Location = New Point(20, 150)
          .AutoSize = True
          .Font = New Font("Arial", 24, FontStyle.Regular)
        End With
    
        Controls.AddRange({p, Button1, lab2})
        p.Controls.Add(lab)
        NewTest()
      End Sub
      Sub SetText(s As String, f As Font)
        With lab
          .Text = s
          .Font = f
        End With
        If lab.Right < p.Right - f.SizeInPoints Then
          Dim fs As Single = CSng(f.SizeInPoints + 0.01)
          SetText(s, New Font(f.FontFamily.Name, fs, f.Style))
        End If
        p.Height = lab.Height + 20
      End Sub
      Sub NewTest()
        Dim f As New Font("Calabri", r.Next(10, 36), FontStyle.Regular)
        SetText(TestStrings(r.Next(0, TestStrings.Count)), f)
        lab2.Text = f.SizeInPoints.ToString
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        NewTest()
      End Sub
    End Class



    Regards Les, Livingston, Scotland

    Thursday, March 26, 2020 10:31 PM
  • Hi Leshay,

    For some reason I am getting this error code when I try to run your code snippet

    Thursday, March 26, 2020 10:56 PM
  • Hi Leshay,

    For some reason I am getting this error code when I try to run your code snippet

    Hi

    Maybe it is already running ........


    Regards Les, Livingston, Scotland

    Thursday, March 26, 2020 11:07 PM
  • Hi Leshay

    Very strange I don't see it running at all.  I even jumped out of visual basic 2019 and started it up again and it still happens! Yet when I go to run other apps it all runs fine.  You have been very helpful and I will continue to try and conquer tis mess <s>

    Thanks,

    Les

    Friday, March 27, 2020 1:31 AM
  • Les2011,

    Shut down your computer and start again, probably you had a stop which did not end the program completely and it is still running as a orphan.


    Success
    Cor

    Friday, March 27, 2020 10:15 AM
  • Hi Cor,

    You are absolutely correct something was still running??  I am not sure how that happened.  Is there something I should add to my code to ensure this does not happen again?

    Thanks

    Friday, March 27, 2020 4:36 PM
  • @Les2011

    The white space at the end that you describe in your first post is almost certainly caused by a typo in your code.

    When you draw your text, you specify the font as:
          New Font("Calibri", 24, FontStyle.Regular)

    However, when you measure the size of the text, you specify the font as:
          f1 = New Font("calabri", 24, FontStyle.Regular)

    Different fonts usually have different metrics. It's unlikely you have a font named "calabri" on your system, so MS Sans Serif will be substituted by default, and that will definitely have different metrics to the Calibri Font that you ultimately use to render the text.




    • Proposed as answer by Cor Ligthert Friday, March 27, 2020 5:18 PM
    • Marked as answer by Les2011 Saturday, March 28, 2020 5:12 AM
    Friday, March 27, 2020 5:02 PM
  • About the stop, are you sure you have nowhere something as End or Application.Exit in your program. 

    That should be done by the application framework which is done by a hidden module and therefore only stop with close.

    Did you see the reply from S P C, seems to me a great catch I did not notice it either. 


    Success
    Cor

    • Marked as answer by Les2011 Saturday, March 28, 2020 5:15 AM
    Friday, March 27, 2020 5:21 PM
  • Hi Leshay,

    I am still a bit confused as to why what I am trying to do seems impossible.  I have put a picture together trying to demonstrate what and how I am getting this accomplished.  BTW my code does work the way I want it to but just was having difficulty in understanding the extra space that appears to get added to my graphic text.  Hope this makes my approach a bit more easy to understand.

    Friday, March 27, 2020 11:32 PM
  • Hi SPC,

    Wow you are quite a detective!! I misspelled Calibri.  Let me play with that and I bet that takes care of the extra space! That's what I needed to understand.  It was killing me not to have a logical explanation for that kind of effect.

    • Marked as answer by Les2011 Saturday, March 28, 2020 6:53 AM
    • Unmarked as answer by Les2011 Saturday, March 28, 2020 6:53 AM
    Saturday, March 28, 2020 5:12 AM
  • Hi Cor,

    You know I never added an application.exit to my program! Just totally forgot about that.

    Thanks so much

    Les

    Saturday, March 28, 2020 5:14 AM
  • Hi

    I would be intrigued to try out your final working code (just the part relevant to this thread)


    Regards Les, Livingston, Scotland

    Saturday, March 28, 2020 10:30 AM
  • Hi Leshay

    Here is my code let me know what you think

    Public Class Form1
        Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
            Dim maxX, maxY As Single
            Dim i As Integer
            Dim backbuffer As New Bitmap(Panel1.Width, Panel1.Height)
            Dim t(5) As String
            Dim tx(5), ty(5) As Single
            Dim f1 As New Font("calibri", 24, FontStyle.Regular)
            Dim stringsize As SizeF
            t(1) = "This is a test 123456789abcdefghijklmnop"
            t(2) = "Leshay"
            t(3) = "Experimental code"
            t(4) = "Hope this works"
            t(5) = "Hope you like this demo"
            Using g As Graphics = Graphics.FromImage(backbuffer)
                For i = 1 To 5
                    stringsize = g.MeasureString(t(i), f1)
                    If i <> 1 Then
                        ty(i) = stringsize.Height + ty(i - 1)
                    End If
                    If stringsize.Width > maxX Then
                        maxX = stringsize.Width
                    End If
                    maxY = maxY + stringsize.Height
                Next i
            End Using
            If maxX <= Panel1.Width Then
                PictureBox1.Width = Panel1.Width
            Else
                PictureBox1.Width = CInt(maxX)
            End If
            If maxY <= Panel1.Height Then
                PictureBox1.Height = Panel1.Height
            Else
                PictureBox1.Height = CInt(maxY)
            End If
            backbuffer = New Bitmap(CInt(PictureBox1.Width), CInt(PictureBox1.Height))
            Using g As Graphics = Graphics.FromImage(backbuffer)
                g.Clear(Color.White) 'paint the buffer white
                For i = 1 To 5
                    g.DrawString(t(i), f1, New SolidBrush(Color.Black), 0, ty(i))
                Next i
            End Using
            PictureBox1.Image = backbuffer
        End Sub

    You need to place a picturebox inside a panel and set the picturebox to 0,0 and a command button.  Picturebox sze (247,151) and panelsize (294,186)

    HTW


    • Edited by Les2011 Saturday, March 28, 2020 8:09 PM
    Saturday, March 28, 2020 7:58 PM
  • Hi

    Thanks Les, It is different to what I thought you wanted, but that is probably my lack of understanding.

    It's always easy once you know how 😂


    Regards Les, Livingston, Scotland

    Saturday, March 28, 2020 8:24 PM
  • Les2011,

    You can try MeasureText it is a bit tighter.

    I did this and got side tracked.

    The example makes the controls. Just cut and paste the code into an empty form. Change the form name as required.

    Public Class Form6
        Private Panel1 As New Panel With {.Parent = Me, .Size = New Size(200, 40),
            .Location = New Point(20, 10), .BackColor = Color.Red, .AutoScroll = True}
        Private PictureBox1 As New PictureBox With {.Parent = Panel1, .Size = New Size(200, 10),
            .Location = New Point(0, 0)}
    
        Private Panel2 As New Panel With {.Parent = Me, .Size = New Size(200, 80),
                .Location = New Point(20, 70), .BackColor = Color.Red, .AutoScroll = True}
        Private PictureBox2 As New PictureBox With {.Parent = Panel2, .Size = New Size(200, 50),
                .Location = New Point(0, 0)}
    
        Private Panel3 As New Panel With {.Parent = Me, .Size = New Size(200, 100),
                .Location = New Point(20, 170), .BackColor = Color.Red, .AutoScroll = True}
        Private PictureBox3 As New PictureBox With {.Parent = Panel3, .Size = New Size(200, 50),
                .Location = New Point(0, 0)}
    
        Private t As String = "This is a test 01234567890123456789k"
        Private f As New Font("calabri", 24, FontStyle.Regular)
    
        Private Sub Form8_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ClientSize = New Size(300, 300)
            Text = "MeasureText"
            SetPicText(Panel1, PictureBox1, t, f)
            SetPicText(Panel2, PictureBox2, t, f)
            SetPicText(Panel3, PictureBox3, t, New Font("arial", 14, FontStyle.Regular))
    
        End Sub
    
        Private Sub SetPicText(thisPanel As Panel, thisPictureBox As PictureBox,
                               t As String, f As Font)
    
            Dim backbuffer As New Bitmap(10, 10)
    
            Using g As Graphics = Graphics.FromImage(backbuffer)
    
                Dim stringsize As Size = TextRenderer.MeasureText(g, t, f,
                        New Size(Integer.MaxValue, Integer.MaxValue),
                        TextFormatFlags.NoPadding)
    
                If stringsize.Width < thisPanel.ClientSize.Width Then
                    thisPictureBox.Width = thisPanel.ClientSize.Width
                Else
                    thisPictureBox.Width = CInt(stringsize.Width)
                End If
    
                If stringsize.Height < thisPanel.ClientSize.Height Then
                    thisPictureBox.Height = thisPanel.ClientSize.Height
                Else
                    thisPictureBox.Height = CInt(stringsize.Height)
                End If
    
                backbuffer = New Bitmap(thisPictureBox.ClientSize.Width,
                                        thisPictureBox.ClientSize.Height)
    
                Dim rect As Rectangle = New Rectangle(0, 0,
                    stringsize.Width - 1, stringsize.Height - 1)
    
                Using g2 As Graphics = Graphics.FromImage(backbuffer)
                    g2.Clear(Color.White)
                    TextRenderer.DrawText(g2, t, f, New Point(0, 0), Color.Black, TextFormatFlags.NoPadding)
                    g2.DrawRectangle(Pens.Red, rect)
                End Using
            End Using
    
            If thisPictureBox.Image IsNot Nothing Then thisPictureBox.Image.Dispose()
    
            thisPictureBox.Image = backbuffer
    
        End Sub
    End Class

    Friday, April 17, 2020 9:56 AM