我对编程图形很陌生,更不用说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()
答案 0 :(得分:2)
您可以将图像清除为透明,然后在每个方向上绘制比图像小2个像素的背景矩形:
Drawing.Clear(Color.Transparent)
Drawing.FillRectangle(New SolidBrush(bgcolor), 2, 2, 96, 96)
'Draw text ...
答案 1 :(得分: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)