我在这里找到了这段代码:
Private _BackgroundColours As New List(Of String)() From { _
"339966", _
"3366CC", _
"CC33FF", _
"FF5050" _
}
Public Function GenerateRactangle(firstName As String, lastName As String) As MemoryStream
Dim imgSize() As Integer = {800, 800}
Dim avatarString As String = String.Format("{0}{1}", firstName(0), lastName(0)).ToUpper()
Dim bgColour = _BackgroundColours(New Random().[Next](0, _BackgroundColours.Count - 1))
Dim bmp As Bitmap = New Bitmap(imgSize(0), imgSize(1))
Dim sf As StringFormat = New StringFormat()
Dim ms As MemoryStream = New MemoryStream()
Dim font As Font = New Font("Arial", 172, FontStyle.Bold, GraphicsUnit.Pixel)
Dim graphics__1 As Graphics = Nothing
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
graphics__1 = Graphics.FromImage(bmp)
graphics__1.Clear(DirectCast(New ColorConverter().ConvertFromString("#" + bgColour), Color))
graphics__1.SmoothingMode = SmoothingMode.AntiAlias
graphics__1.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
graphics__1.DrawString(avatarString, font, New SolidBrush(Color.WhiteSmoke), New RectangleF(0, 0, imgSize(0), imgSize(1)), sf)
graphics__1.Flush()
bmp.Save(ms, ImageFormat.Png)
Return ms
End Function
在stackoverflow上,效果很好。但是,我需要在背景中使用透明的 PNG 图像,并改变背景颜色。
它目前的样子:
我正在寻找它的样子:
添加的PNG图像是这样的:
我希望对图形调用有更多了解的人可以让我知道如何去做。