1

I'm quite new to programming graphics, much less with VB so I'm running to a wall here. I've done basically all the code all I'm missing is to add a few pixels of transparent padding/border around the image and I got stuck. I looked around but the examples I've seen seemed overwhelmingly complex and looked to me like an overkill (pages upon pages of code).

Any pointers or an easy to understand tutorials/examples would be extremely appreciated.

Appended the current code below:

Current code

Dim img As Image = New Bitmap(100, 100)

Dim Drawing As Graphics = Graphics.FromImage(img)
Dim rand As New Random
Dim bgcolor As Color = Color.FromArgb(rand.Next(64, 196), rand.Next(64, 196), rand.Next(64, 196))

Dim family As FontFamily = Nothing

Dim fontName As String = "Lucida Sans Typewriter"
Dim fontSize As Single = 42

Using fontTester As New Font(fontName, fontSize, FontStyle.Regular, GraphicsUnit.Pixel)
    If fontTester.Name = fontName Then 
        family = New FontFamily("Lucida Sans Typewriter")
    Else
        Try
            Dim privateFonts As New System.Drawing.Text.PrivateFontCollection()
            privateFonts.AddFontFile(HttpContext.Current.Server.MapPath("~/") + "\styles\fonts\LTYPE.ttf")
            Dim font As New System.Drawing.Font(privateFonts.Families(0), 42)
            family = font.FontFamily
        Catch ex As Exception
            family = New FontFamily("Arial")
        End Try
    End If
End Using

Dim FontText As Font = New Font(family, 42, FontStyle.Regular)

Drawing.Clear(bgcolor)
Dim textBrush As Brush = New SolidBrush(Color.White)
Drawing.DrawString(initials, FontText, textBrush, 7, 16)
Drawing.Save()
textBrush.Dispose()
Drawing.Dispose()
Paradox Code
  • 148
  • 2
  • 14

2 Answers2

2

You can do so by clearing the image to transparent and then drawing a background rectangle that is 2 pixels smaller than the image in every direction:

Drawing.Clear(Color.Transparent)
Drawing.FillRectangle(New SolidBrush(bgcolor), 2, 2, 96, 96)
'Draw text ...
Nico Schertler
  • 32,049
  • 4
  • 39
  • 70
  • Thank you, this was really easy (why I haven't thought of it is beyond me at the moment) and straight to the point. It works perfectly. – Paradox Code Sep 18 '15 at 13:05
0

The following code is an example the changes one pixel around the black font to transparent. It should be easy to extend the code to consider more than one pixel. This code works if your image is not in a rectangular shape like a text string as it is shown in your sample code. Even setting the font color to black, there are some pixels that are not pure black, they have some shades, so in my example i check just if the Red component is 255 to account for the gray shades.

        Dim bmp As New Bitmap(width, height)
        Dim g As Graphics = Graphics.FromImage(bmp)
        Dim rand As New Random
        Dim bgcolor As Color = Color.Red
        g.Clear(bgcolor)
        Dim FontText As Font = New Font("Arial", 42, FontStyle.Regular)

        Dim textBrush As Brush = New SolidBrush(Color.Black)
        g.DrawString("Teste", FontText, textBrush, 7, 16)
        For i As Integer = 0 To Width - 1
            For j As Integer = 0 To Height - 1
               If bmp.GetPixel(i, j).R < 255 Then
                    If (bmp.GetPixel(i - 1, j).R = 255 And bmp.GetPixel(i - 1, j).G = 0 And bmp.GetPixel(i - 1, j).B = 0) Then
                        bmp.SetPixel(i, j, Color.Transparent)
                    End If
                    If (bmp.GetPixel(i + 1, j).R = 255 And bmp.GetPixel(i + 1, j).G = 0 And bmp.GetPixel(i + 1, j).B = 0) Then
                        bmp.SetPixel(i, j, Color.Transparent)
                    End If
                    If (bmp.GetPixel(i, j - 1).R = 255 And bmp.GetPixel(i, j - 1).G = 0 And bmp.GetPixel(i, j - 1).B = 0) Then
                        bmp.SetPixel(i, j, Color.Transparent)
                    End If
                    If (bmp.GetPixel(i, j + 1).R = 255 And bmp.GetPixel(i, j + 1).G = 0 And bmp.GetPixel(i, j + 1).B = 0) Then
                        bmp.SetPixel(i, j, Color.Transparent)
                    End If
                    If (bmp.GetPixel(i + 1, j + 1).R = 255 And bmp.GetPixel(i + 1, j + 1).G = 0 And bmp.GetPixel(i + 1, j + 1).B = 0) Then
                        bmp.SetPixel(i, j, Color.Transparent)
                    End If
                    If (bmp.GetPixel(i - 1, j - 1).R = 255 And bmp.GetPixel(i - 1, j - 1).G = 0 And bmp.GetPixel(i - 1, j - 1).B = 0) Then
                        bmp.SetPixel(i, j, Color.Transparent)
                    End If
                    If (bmp.GetPixel(i + 1, j - 1).R = 255 And bmp.GetPixel(i + 1, j - 1).G = 0 And bmp.GetPixel(i + 1, j - 1).B = 0) Then
                        bmp.SetPixel(i, j, Color.Transparent)
                    End If
                    If (bmp.GetPixel(i - 1, j + 1).R = 255 And bmp.GetPixel(i - 1, j + 1).G = 0 And bmp.GetPixel(i - 1, j + 1).B = 0) Then
                        bmp.SetPixel(i, j, Color.Transparent)
                    End If
                   End If

            Next
        Next

        textBrush.Dispose()
        g.Dispose()

        bmp.Save("TESTE.png", System.Drawing.Imaging.ImageFormat.Png)
mago
  • 56
  • 6