none
C# How to draw bold big size text at center on picturebox RRS feed

  • Question

  • i got a link  http://vbcity.com/forums/t/27770.aspx which is close to my requirement but the code is in vb.net and i am not good in vb.net.

    so anyone please see the code from link i shared here and give me a fresh code in c# which will draw bold big size text on picturebox at center. thanks

    one code in vb.net

    'This class is used so that you can set everything.
    Public Class DrawStringOnPictureBoxParams
    
        Public [Text] As String 'Text displayed on PictureBox
        Public [PictureBox] As PictureBox 'PictureBox to draw to
        Public [MaxSize] As Single 'max font size
        Public [Font] As String 'font string
        Public [bgColor] As Color 'background color of picturebox
        Public [foreColor] As Color 'color of text drawn on picturebox
        Public [PixelFormat] As Drawing.Imaging.PixelFormat 'Pixel format of the image
        Public [StringFormat] As StringFormat 'String format (center and such)
        Public [LayoutSizeF] As SizeF 'Not sure...
        Public [BorderSize] As Single 'space between the text and the edge of the picturebox
        Public [X] As Integer 'x position to place text
        Public [Y] As Integer 'y position to place text
    
        Private Sub New()
            Me.Text = ""
            Me.PictureBox = Nothing
            Me.MaxSize = 72
            Me.Font = "Comic Sans MS"
            Me.bgColor = Color.Black
            Me.foreColor = Color.Red
            Me.PixelFormat = Drawing.Imaging.PixelFormat.Format24bppRgb
            Me.StringFormat = New StringFormat()
            Me.StringFormat.Alignment = StringAlignment.Center
            Me.StringFormat.LineAlignment = StringAlignment.Center
            Me.LayoutSizeF = New SizeF(1000, 1000)
            Me.BorderSize = 4
            Me.X = 0
            Me.Y = 0
        End Sub
    
        Public Sub New(ByVal txt As String, ByRef pb As PictureBox)
            Me.New()
            Me.Text = txt
            Me.PictureBox = pb
            Me.X = Me.PictureBox.Width / 2
            Me.Y = Me.PictureBox.Height / 2
        End Sub
    
        Public Sub New(ByVal txt As String, ByRef pb As PictureBox, ByVal maxSize As Single)
            Me.New(txt, pb)
            Me.MaxSize = maxSize
        End Sub
    
        Public Sub New(ByVal txt As String, ByRef pb As PictureBox, ByVal maxSize As Single, ByVal font As String)
            Me.New(txt, pb, maxSize)
            Me.Font = font
        End Sub
    
        Public Sub New(ByVal txt As String, ByRef pb As PictureBox, ByVal maxSize As Single, ByVal font As String, ByVal bgColour As Color, ByVal foreColour As Color)
            Me.New(txt, pb, maxSize, font)
            Me.bgColor = bgColour
            Me.foreColor = foreColour
        End Sub
    
        Public Sub New(ByVal txt As String, ByRef pb As PictureBox, ByVal maxSize As Single, ByVal font As String, ByVal bgColour As Color, ByVal foreColour As Color, ByVal pFormat As Imaging.PixelFormat)
            Me.New(txt, pb, maxSize, font, bgColour, foreColour)
            Me.PixelFormat = pFormat
        End Sub
    
        Public Sub New(ByVal txt As String, ByRef pb As PictureBox, ByVal maxSize As Single, ByVal font As String, ByVal bgColour As Color, ByVal foreColour As Color, ByVal pFormat As Imaging.PixelFormat, ByVal sFormat As StringFormat)
            Me.New(txt, pb, maxSize, font, bgColour, foreColour, pFormat)
            Me.StringFormat = sFormat
        End Sub
    
        Public Sub New(ByVal txt As String, ByRef pb As PictureBox, ByVal maxSize As Single, ByVal font As String, ByVal bgColour As Color, ByVal foreColour As Color, ByVal pFormat As Imaging.PixelFormat, ByVal sFormat As StringFormat, ByVal layoutSize As SizeF)
            Me.New(txt, pb, maxSize, font, bgColour, foreColour, pFormat, sFormat)
            Me.LayoutSizeF = layoutSize
        End Sub
    
        Public Sub New(ByVal txt As String, ByRef pb As PictureBox, ByVal maxSize As Single, ByVal font As String, ByVal bgColour As Color, ByVal foreColour As Color, ByVal pFormat As Imaging.PixelFormat, ByVal sFormat As StringFormat, ByVal layoutSize As SizeF, ByVal borderSize As Single)
            Me.New(txt, pb, maxSize, font, bgColour, foreColour, pFormat, sFormat, layoutSize)
            Me.BorderSize = borderSize
        End Sub
    
        Public Sub New(ByVal txt As String, ByRef pb As PictureBox, ByVal maxSize As Single, ByVal font As String, ByVal bgColour As Color, ByVal foreColour As Color, ByVal pFormat As Imaging.PixelFormat, ByVal sFormat As StringFormat, ByVal layoutSize As SizeF, ByVal borderSize As Single, ByVal x As Integer, ByVal y As Integer)
            Me.New(txt, pb, maxSize, font, bgColour, foreColour, pFormat, sFormat, layoutSize, borderSize)
            Me.X = x
            Me.Y = y
        End Sub
    End Class
    
    'This is the sub that does the drawing...
    Public Sub DrawStringOnPictureBox(ByRef p As DrawStringOnPictureBoxParams)
    
        'we start with a new bitmap (width, height, pixelformat)
        Dim i As New Bitmap(p.PictureBox.Width, p.PictureBox.Height, p.PixelFormat)
    
        'we get a Graphics object from that image
        Dim g As Graphics = Graphics.FromImage(i)
    
        'this is how you set the background color of the image
        g.Clear(p.bgColor)
    
        'Set the drawing font
        Dim f As Font = New Font(p.Font, p.MaxSize, FontStyle.Regular, GraphicsUnit.World)
    
        'Measure the text (text, font, layoutsize, stringformat)
        Dim fSize As SizeF = g.MeasureString(p.Text, f, p.LayoutSizeF, p.StringFormat)
    
        'Loop until the text width is less than the picturebox width (plus the border)
        Do While fSize.Width > (p.PictureBox.Width - p.BorderSize)
            'if it is wider then we decriminate the text size by one and try again
            p.MaxSize -= 1
            f = New Font(p.Font, p.MaxSize, FontStyle.Regular, GraphicsUnit.World)
            fSize = g.MeasureString(p.Text, f, p.LayoutSizeF, p.StringFormat)
        Loop
    
        'Loop until the text height is less than the picturebox height (plus the border)
        Do While fSize.Height > (p.PictureBox.Height - p.BorderSize)
            'if it is heigher then we decriminate the text size again my one
            p.MaxSize -= 1
            f = New Font(p.Font, p.MaxSize, FontStyle.Regular, GraphicsUnit.World)
            fSize = g.MeasureString(p.Text, f, p.LayoutSizeF, p.StringFormat)
        Loop
    
        'This is the DrawString method that draws the text onto the image
        'DrawString(text, font, fontsize, fontsyle, graphicsunit, Brush, XPos, YPos, StringFormat)
        g.DrawString(p.Text, f, New SolidBrush(p.foreColor), p.X, p.Y, p.StringFormat)
    
        'set the Image of our Picturebox to the bitmap
        p.PictureBox.Image = i
    
        'cleanup
        g.Dispose()
    End Sub


    • Edited by Sudip_inn Monday, February 20, 2017 9:40 AM
    Friday, February 17, 2017 2:00 PM

Answers

  • For example =>

       private void pictureBox1_Paint(object sender, PaintEventArgs e)
            {
                string sString = "Test Text Line 1\r\nTest Text Line 2"; 
                using (Font font = new Font("Arial", 14, System.Drawing.FontStyle.Bold, GraphicsUnit.Point))
                {
                    TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak;
                    TextRenderer.DrawText(e.Graphics, sString, font, pictureBox1.ClientRectangle, Color.Red, flags);
                }
            }

    • Edited by Castorix31 Friday, February 17, 2017 2:44 PM
    • Marked as answer by Sudip_inn Monday, February 20, 2017 9:39 AM
    Friday, February 17, 2017 2:42 PM

All replies

  • For example =>

       private void pictureBox1_Paint(object sender, PaintEventArgs e)
            {
                string sString = "Test Text Line 1\r\nTest Text Line 2"; 
                using (Font font = new Font("Arial", 14, System.Drawing.FontStyle.Bold, GraphicsUnit.Point))
                {
                    TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak;
                    TextRenderer.DrawText(e.Graphics, sString, font, pictureBox1.ClientRectangle, Color.Red, flags);
                }
            }

    • Edited by Castorix31 Friday, February 17, 2017 2:44 PM
    • Marked as answer by Sudip_inn Monday, February 20, 2017 9:39 AM
    Friday, February 17, 2017 2:42 PM
  • I can not follow the picturebox pain event approach because based on some condition i have to write some text on pictute box. So advise me some other approach.
    Saturday, February 18, 2017 2:20 PM
  • All the drawing of windows/controls is done in Paint event (WM_PAINT)

    You can draw for example several texts with transparency, bitmaps, etc...

    Saturday, February 18, 2017 3:00 PM