1

我对图形编程很陌生,更不用说VB了,所以我在这里碰壁了。我基本上已经完成了所有我所缺少的代码,只是在图像周围添加了一些透明填充/边框像素,但我被卡住了。我环顾四周,但我看到的示例似乎非常复杂,在我看来就像是矫枉过正(一页一页的代码)。

任何指针或易于理解的教程/示例将不胜感激。

在下面附加了当前代码:

当前代码

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

2 回答 2

2

为此,您可以将图像清除为透明,然后在各个方向上绘制一个比图像小 2 像素的背景矩形:

Drawing.Clear(Color.Transparent)
Drawing.FillRectangle(New SolidBrush(bgcolor), 2, 2, 96, 96)
'Draw text ...
于 2015-09-18T12:46:46.767 回答
0

以下代码是将黑色字体周围的一个像素更改为透明的示例。扩展代码以考虑多个像素应该很容易。如果您的图像不是像示例代码中所示的文本字符串那样的矩形,则此代码有效。即使将字体颜色设置为黑色,也有一些像素不是纯黑色,它们有一些阴影,所以在我的示例中,我检查红色分量是否为 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)
于 2015-09-18T13:47:31.897 回答