Introduction
There are many situation when require to create dynamic image with text. It is possible to create dynamic image with text and we can create the image with particular height and width.
Main
For doing this we require to import three Namespaces (System.Drawing, System.Drawing.Text, System.Drawing.Imaging) .
Below find the complete function that creates the image on the fly:
Public Shared Function WriteImage(ByVal Text As String, ByVal BackgroundColor As Color, ByVal iWidth As Int16, ByVal iHeight As Int16) As Bitmap
1) Dim oBitmap As Bitmap = New Bitmap(iWidth, iHeight)
2) Dim oGraphic As Graphics = Graphics.FromImage(oBitmap)
3) Dim oColor As System.Drawing.Color
4) Dim sText As String = Text
5) Dim sFont As String = "Black"
6) oColor = BackgroundColor
7) Dim oBrush As New SolidBrush(oColor)
8) Dim oBrushWrite As New SolidBrush(Color.Black)
9) oGraphic.FillRectangle(oBrush, 0, 0, iWidth, iHeight)
10) oGraphic.TextRenderingHint = TextRenderingHint.AntiAlias
11 Dim oFont As New Font(sFont, 8)
12) Dim oPoint As New Point(5, 5)
13) oGraphic.DrawString(sText, oFont, oBrushWrite, oPoint)
14) Return oBitmap
End Function
Explanation:
WriteImage function take 4 parameters
1) Text : Text to be written on image.
2) BackgroundColor: Background color of the image.
3) iWidth: Width of the image.
4) iHeight: Height of the image.
Line 1: First we have created the object of Bitmap of require size.
Line 2:Then create the graphics for bitmap image.
Line 3: Create the object of Color.
Line 4: Create the object of String type and assign the Text Parameter value.
Line 5: Create the object of String type and assign the Font value for Text.
Line 6: Assign the color for brush.
Line 7: Create the object for brush using color parameter.
Line 8: Create the object of SolidBrush.
Line9: Create the rectangle using grapich.
Line 10: Define TextRenderingHint for graphics.
Line11 and Line 12: Crate the object of Font and Point.
Line 13 : Write the text on image using graphics object.
Download Source Code
Other Related and Popular Articles :
Author Profile : Sandeep Dubey
How would you rate the quality of this content?
Poor
Excellent
Comments
Leave New Comments
Article Content copyright by
Sandeep Dubey
Everything else Copyright © by
WorldofASP.NET
2009