Feedback

Text auf Bild zeichnen

Sprache: VB

Mit dieser Funktion kann man einen Text auf ein Bild zeichnen wobei man zusätzlich noch Farbe, Schriftart und Position bestimmen kann.
Imports System.Drawing.Imaging
Imports System.Drawing

Private Sub TextOnImage(ByVal OldImage As String, ByVal NewImage As String, ByVal Text As String, ByVal Format As ImageFormat, ByVal Font As Font, ByVal Color As Color, ByVal Position As Point)
    Dim TmpSize As System.Drawing.Size
    Dim Image As Image = System.Drawing.Image.FromFile(OldImage)
    Dim Brush As New SolidBrush(Color)

    'Größe auslesen
    TmpSize.Height = Image.Height
    TmpSize.Width = Image.Width

    'Neue Bitmap erstellen
    Dim NewBitmap As New System.Drawing.Bitmap(Image, TmpSize)

    'Neue Grafik erstellen anhand der Bitmap
    Dim Graphic As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(NewBitmap)

    'String auf Bild zeichnen
    Graphic.DrawString(Text, Font, Brush, Position)

    'Neues Bild speichern
    NewBitmap.Save(NewImage, Format)

    'Ressourcen freigeben
    Graphic.Dispose()
    NewBitmap.Dispose()
End Sub
Imports System.Drawing.Imaging
Imports System.Drawing

Private Sub TextOnImage(ByVal OldImage As String, ByVal NewImage As String, ByVal Text As String, ByVal Format As ImageFormat, ByVal Font As Font, ByVal Color As Color, ByVal Position As Point)
    Dim TmpSize As System.Drawing.Size
    Dim Image As Image = System.Drawing.Image.FromFile(OldImage)
    Dim Brush As New SolidBrush(Color)

    'Größe auslesen
    TmpSize.Height = Image.Height
    TmpSize.Width = Image.Width

    'Neue Bitmap erstellen
    Dim NewBitmap As New System.Drawing.Bitmap(Image, TmpSize)

    'Neue Grafik erstellen anhand der Bitmap
    Dim Graphic As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(NewBitmap)

    'String auf Bild zeichnen
    Graphic.DrawString(Text, Font, Brush, Position)

    'Neues Bild speichern
    NewBitmap.Save(NewImage, Format)

    'Ressourcen freigeben
    Graphic.Dispose()
    NewBitmap.Dispose()
End Sub