VB.NET 在动态创建的 png 图像周围绘制两个像素的透明边框?

VB.NET Drawing a two pixel transparent border around an on the fly created png image?

我对图形编程还很陌生,VB 更不用说了,所以我 运行 在这里是一堵墙。我基本上已经完成了所有我缺少的代码,就是在图像周围添加几个透明 padding/border 像素,但我被卡住了。我环顾四周,但我看到的例子似乎非常复杂,在我看来就像是矫枉过正(一页又一页的代码)。

任何指示或易于理解的 tutorials/examples 将不胜感激。

在下面附加当前代码:

当前代码

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()

您可以通过将图像清除为透明,然后绘制一个在每个方向上都比图像小 2 像素的背景矩形来实现:

Drawing.Clear(Color.Transparent)
Drawing.FillRectangle(New SolidBrush(bgcolor), 2, 2, 96, 96)
'Draw text ...

以下代码是将黑色字体周围的一个像素更改为透明的示例。扩展代码以考虑多个像素应该很容易。如果您的图像不是像示例代码中所示的文本字符串那样的矩形,则此代码有效。即使将字体颜色设置为黑色,也有一些像素不是纯黑色,它们有一些阴影,所以在我的示例中,我检查红色分量是否为 255 以说明灰色阴影。

        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)