将图像,背景和文本合并到单个图像中 [英] Merge an image, a background and Text into a single image

查看:225
本文介绍了将图像,背景和文本合并到单个图像中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 私人_BackgroundColours作为新列表(字符串)()从{_ 
339966,_
3366CC,_
CC33FF,_
FF5050_
}

公共函数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.From图片(BMP)
graphics__1.Clear(DirectCast(新ColorConverter()。ConvertFromString( # + bgColour),彩色))
graphics__1.SmoothingMode = SmoothingMode.AntiAlias
graphics__1.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
graphics__1.DrawString(avatarString,字体,新SolidBrush(Color.WhiteSmoke),新的RectangleF(0,0,imgSize(0),imgSize(1)),SF)
graphics__1.Flush ()
bmp.Save(ms,ImageFormat.Png)

返回ms
End Function

On



我正在寻找它的样子:







我希望有更多关于图形调用的知识可以让我知道如何去做这件事。

解决方案

我你发现叶子至少是 Font Grahics 对象没有处理,所以如果它被用作工厂来处理多个图像,它会泄漏。诸如挑选随机背景颜色可能更适合调用代码,并且memstream似乎是一种奇怪的返回类型选择。



创建背景的一般方法,覆盖一个PNG并将文本应用到它:

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

'fixed size?
Dim bmp As New Bitmap(250,250)
使用g As Graphics = Graphics.FromImage(bmp)
使用br作为新的SolidBrush(bg)
g.FillRectangle(br ,0,0,bmp.Width,bmp.Height)
结束使用
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.CompositingQuality = CompositingQuality.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAlias
g.SmoothingMode = SmoothingMode.HighQuality
g.DrawImage(av,0,bmp.Width,bmp.Height)

'最后是文本,以新图像为中心
'也可以绘制到以IT
为中心的AV中使用fnt作为新字体(Arial,32,FontStyle.Bold,GraphicsUnit.Pixel)
TextRenderer .DrawText(g,text,fnt,New Rectangle(0,0,250,250),
Color.WhiteSmoke,
TextFormatFlags.Horizo​​ntalCenter或TextFormatFlags.VerticalCenter)
End使用

结束使用

返回bmp
结束函数

示例用法:

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

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

av.Dispose()

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

您可能想要传递或设置其他参数,例如所需的大小,字体大小甚至文本颜色。尽管如此,我还是会让它成为一个类,所以如果它被用来处理它们中的很多,那么许多参数可以设置一次。



结果:



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

I found this code here:

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

On stackoverflow and it works great. However, I am in need of using a transparent PNG image in the background with the color changing background color.

What it currently looks like:

What I am looking for it to look like:

With the PNG image being this that was added:

I am hoping someone with more knowledge about the graphics calls can let me know how to go about doing this.

解决方案

The method you found leaves at least the Font and Grahics objects undisposed, so if that is used as a factory to process numerous images, it will leak. Things like picking a random back color might be better left to the calling code, and a memstream seems an odd return type choice.

A general method to create a background, overlay a PNG and apply the text to it:

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

Sample usage:

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

When your code is done with it, newImg should also be disposed.

There are other params you might want to be passed or set such as the desired size, font size and maybe even the text color. Passing any more, though and I would make it a class, so if it is used to processed a lot of them, many params could be set once.

Result:

The image created was 250,250, it is displayed in a 150x150 PBox

这篇关于将图像,背景和文本合并到单个图像中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆