VB.NET Add Text to an Image

jim brown 271 Reputation points
2023-03-27T17:03:15.6033333+00:00

So I have this code that saves an image (QR Code Image) and I would like to save text overlay for example add text "QR Code". thoughts?

screenshot



Private Sub btnSaveQRCode_Click(sender As Object, e As EventArgs) Handles btnSaveQRCode.Click

            'Create a new SaveFileDialog
            Dim sfd As New SaveFileDialog

            sfd.Filter = "Jpg Image|*.jpg|Bmp Image|*.bmp|Png Image|*.png"
            sfd.FileName = tbText.Text
            sfd.AddExtension = True 'Make sure the extension is added to the FileName

            If sfd.ShowDialog = DialogResult.OK Then 'If the user presses "Ok" button on the SaveFileDialog

                'MsgBox(sfd.FileName)

                Try
                    Dim img As Image = GetFormImage(True)
                    img.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)

                Catch ex As Exception
                    'Me.Text = ex.Message
                End Try

                Dim fileName = sfd.FileName
                Dim CropRect As New Rectangle(25, 105, 145, 160)
                Dim OriginalImage = Image.FromFile(fileName)
                Dim CropImage = New Bitmap(CropRect.Width, CropRect.Height)
                Using grp = Graphics.FromImage(CropImage)
                    grp.DrawImage(OriginalImage, New Rectangle(0, 0, CropRect.Width, CropRect.Height), CropRect, GraphicsUnit.Pixel)
                    OriginalImage.Dispose()
                    CropImage.Save(fileName)
                End Using

            End If

            sfd.Dispose() 'Dispose the SaveFileDialog when no longer needed

  
    End Sub


  Private Function GetFormImage(ByVal include_borders As Boolean) As Bitmap

        Dim Counter As Integer = 0

        If Me.WindowState = FormWindowState.Minimized Then
            Me.Show()
            Me.WindowState = FormWindowState.Normal
            Counter = 1
        End If

        ' Make the bitmap.
        Dim wid As Integer = Me.Width
        Dim hgt As Integer = Me.Height
        Dim bm As New Bitmap(wid, hgt)

        ' Draw the form onto the bitmap.
        Me.DrawToBitmap(bm, New Rectangle(0, 0, wid, hgt))

        ' Make a smaller bitmap without borders.
        wid = Me.ClientSize.Width
        hgt = Me.ClientSize.Height
        Dim bm2 As New Bitmap(wid, hgt)

        ' Get the offset from the window's corner to its client
        ' area's corner.
        Dim pt As New Point(0, 0)
        pt = PointToScreen(pt)
        Dim dx As Integer = pt.X - Me.Left
        Dim dy As Integer = pt.Y - Me.Top

        ' Copy the part of the original bitmap that we want
        ' into the bitmap.
        Dim gr As Graphics = Graphics.FromImage(bm2)
        gr.DrawImage(bm, 0, 0, New Rectangle(dx, dy, wid, hgt), GraphicsUnit.Pixel)

        If Counter = 1 Then
            Me.WindowState = FormWindowState.Minimized
            Counter = 0
        End If

        Return bm

    End Function
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,904 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,206 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,741 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 85,956 Reputation points
    2023-03-27T21:01:10.3+00:00

    You can do something like this :

            Dim img As Drawing.Image = Drawing.Image.FromFile("E:\Butterfly_small.png")
            Dim sText = "This is a text"
            Dim nWidth As Integer = img.Width
            Dim nHeight As Integer = img.Height
            Dim nTextHeight = 50
    
            Dim bmp As Bitmap = New Bitmap(nWidth, nHeight + nTextHeight, Drawing.Imaging.PixelFormat.Format32bppArgb)
    
            Using g As Graphics = Graphics.FromImage(bmp)
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias
                g.DrawImage(img, New Rectangle(New Point(), img.Size), New Rectangle(New Point(), img.Size), GraphicsUnit.Pixel)
                Using font As Font = New Font("Arial", 16, Drawing.FontStyle.Regular, GraphicsUnit.Point)
                    Dim rect As Rectangle = New Rectangle(0, nHeight, nWidth, nTextHeight)
                    g.FillRectangle(Drawing.Brushes.White, rect)
                    'g.FillRectangle(Drawing.Brushes.Transparent, rect)
                    'g.DrawRectangle(Pens.Black, rect)
                    Dim stringFormat As StringFormat = New StringFormat()
                    stringFormat.Alignment = StringAlignment.Center
                    stringFormat.LineAlignment = StringAlignment.Center
                    g.DrawString(sText, font, Brushes.Black, rect, stringFormat)
                End Using
            End Using
    
            bmp.Save("E:\ButterflyWithText.png", Drawing.Imaging.ImageFormat.Png)
            ' Test in a PictureBox  
            PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
            PictureBox1.Image = bmp
    
    

    User's image

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.