代码之家  ›  专栏  ›  技术社区  ›  Paradox Code

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

  •  1
  • Paradox Code  · 技术社区  · 9 年前

    我对图形编程很陌生,更不用说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()
    
    2 回复  |  直到 9 年前
        1
  •  2
  •   Nico Schertler    9 年前

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

    Drawing.Clear(Color.Transparent)
    Drawing.FillRectangle(New SolidBrush(bgcolor), 2, 2, 96, 96)
    'Draw text ...
    
        2
  •  0
  •   mago    9 年前

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