write a string in a image using .net code

Thursday 5 March 2015

write a string in a image using .net code

on button  click event write the following code:

 1.take a image folder with in the application
Protected Sub btnConvert_Click(sender As Object, e As System.EventArgs)
        Dim text As String = "write a string "      
        Dim text As String = "write a string 1"       
        Dim text2 As String = "Write another string " 
        Dim bitmap As New Bitmap(1, 1)
        Dim font As New Font("Arial", 15, FontStyle.Regular, GraphicsUnit.Pixel)
        Dim graphics As Graphics = graphics.FromImage(bitmap)
        Dim width As Integer = CInt(graphics.MeasureString(text, font).Width)
        Dim height As Integer = CInt(graphics.MeasureString(text, font).Height) + 40
        bitmap = New Bitmap(bitmap, New Size(width, height))
        graphics = graphics.FromImage(bitmap)
        graphics.Clear(Color.White)
        graphics.SmoothingMode = SmoothingMode.AntiAlias
        graphics.TextRenderingHint = TextRenderingHint.AntiAlias
        graphics.DrawString(text, font, New SolidBrush(Color.Black), 0, 0)
        graphics.DrawString(text1, font, New SolidBrush(Color.Black), 0, 20)
        graphics.DrawString(text2, font, New SolidBrush(Color.Black), 0, 40)
        graphics.Flush()
        graphics.Dispose()
        Dim fileName As String = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) & ".jpg"
        bitmap.Save(Server.MapPath("~/images/") & fileName, ImageFormat.Jpeg)
        imgText.ImageUrl = "~/images/" & fileName
        imgText.Visible = True
    End Sub