将图像、背景和文本合并为单个图像

Merge an image, a background and Text into a single image

我在这里找到这段代码:

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

Whosebug 上,效果很好。但是,我需要在背景中使用透明的 PNG 图像,并且背景颜色会发生变化。

目前的样子:

我想要的样子:

添加的 PNG 图像是这样的:

我希望对图形调用有更多了解的人可以让我知道如何去做。

您找到的方法至少留下 FontGrahics 对象未处理,因此如果将其用作处理大量图像的工厂,它会泄漏。诸如选择随机背景颜色之类的事情最好留给调用代码,而 memstream 似乎是一种奇怪的 return 类型选择。

创建背景、叠加 PNG 并向其应用文本的一般方法:

Private Function CreateLabeledAvatar(av As Image, bg As Color, text As String) As Image

    ' fixed size?
    Dim bmp As New Bitmap(250, 250)
    Using g As Graphics = Graphics.FromImage(bmp)
        Using br As New SolidBrush(bg)
            g.FillRectangle(br, 0, 0, bmp.Width, bmp.Height)
        End Using
        g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        g.CompositingQuality = CompositingQuality.HighQuality
        g.TextRenderingHint = TextRenderingHint.AntiAlias
        g.SmoothingMode = SmoothingMode.HighQuality
        g.DrawImage(av, 0, 0, bmp.Width, bmp.Height)

        ' lastly the text, centred on the new image
        ' could also draw to the AV passed to center on IT
        Using fnt As New Font("Arial", 32, FontStyle.Bold, GraphicsUnit.Pixel)
            TextRenderer.DrawText(g, text, fnt, New Rectangle(0, 0, 250, 250), 
                  Color.WhiteSmoke,
                  TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter)
        End Using

    End Using

    Return bmp
End Function

示例用法:

Dim av = Image.FromFile("C:\temp\maleAV.png")
Dim bg = Color.FromArgb(62, 103, 207)

Dim newImg = CreateLabeledAvatar(av, bg, "BB")
pb1.Image = newImg

av.Dispose()

当您的代码完成后,newImg 也应该被处理掉。

您可能还想传递或设置其他参数,例如所需的大小、字体大小,甚至可能是文本颜色。不过,如果再通过,我会将其设为 class,因此如果它用于处理大量参数,则可以设置一次许多参数。

结果:

创建的图像为 250,250,显示在 150x150 PBox 中