向使用 GDI+ 绘制的图像添加文本

Adding Text to Image Drawn with GDI+

我正在使用计时器和图片框显示图像。这些图像似乎是在彼此叠加而不是显示一个,然后删除那个,然后加载另一个。

显示图片时,我想在图片上叠加文字。

这是我用来创建图像和文本的代码

 Dim fname As String = (Actually an array in images(jpg) that display with timer)
    Dim bm As New Bitmap(fname)
    PicBox.Image = bm
    findex -= 1  (index of image array)
    Dim g As Graphics = PicBox.CreateGraphics
    g.DrawImage(bm, 300, 10)
    g.DrawString("Kishman Tukus", New Font("Arial", 24, FontStyle.Bold), Brushes.Green, 400, 100)
    g.ResetTransform() '   
    g.Dispose()

我需要使用定时器在图片框中一次显示一个图像,我需要叠加 图片上也有文字。

谁能帮我阻止图片添加到图片框,而不是一次显示一张? 或者甚至更好,根本不使用 PictureBox,只显示带有文本叠加的图像? 无论如何,我需要停止记忆流血。 谢谢

我希望看到更多类似的内容:

Dim bm As New Bitmap(fname)
Using g As Graphics = Graphics.FromImage(bm)
    g.DrawString("Kishman Tukus", New Font("Arial", 24, FontStyle.Bold), Brushes.Green, 400, 100)
End Using
PicBox.Image = bm

我认为加载图像的方式很重要。每次显示时,您真的按文件名加载每个图像吗?这是误导,因为你提到有一个数组。区别在于您保留对这些图像的引用并且您正在修改每个图像。请记住这是一个引用类型,因此原始项目会被修改。因此,您最终会重复将文本覆盖在自身之上。具有讽刺意味的是,如果你真的每次都加载图像,那么你实际上不会有这个问题:)

我按照你所拥有的(我认为)制作了一些东西,我们使用 Object.Clone 在内存中制作每个位图的副本,你可以在不修改原始图像的情况下修改它。

Private images As New List(Of Bitmap)()
Dim findex As Integer = 0

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim bmCopy = DirectCast(images(findex).Clone(), Bitmap)

    Using g As Graphics = Graphics.FromImage(bmCopy)
        g.DrawString(Guid.NewGuid().ToString(), New Font("Arial", 24, FontStyle.Bold), Brushes.Green, 400, 100)
    End Using

    PicBox.Image = bmCopy

    findex = (findex + 1) Mod images.Count()
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    images.Add(My.Resources.Image1)
    images.Add(My.Resources.Image2)
    Timer1.Interval = 1000
    Timer1.Enabled = True
End Sub

没有内存泄漏。您可以检查内存增加,但 GC 最终将其清除。