Click here to Skip to main content
15,068,471 members
Please Sign up or sign in to vote.
1.40/5 (5 votes)
See more:
I want to generate a image file from a given string. I did Google and got following code but it is giving error:

VB
Dim Img As Image, Grf As Graphics

        Dim Str As String = "THE STRING"

        Dim Fnt As New Font(Me.Font, FontStyle.Regular)

        'Set the properties of the font (Fnt), e.g. :

        Fnt = New Font(Fnt.FontFamily, 6) 'The size of the font will be small

        Dim TempBmp As New Bitmap(TextRenderer.MeasureText(Str, Fnt).Width, TextRenderer.MeasureText(Str, Fnt).Height)

        Grf = Graphics.FromImage(TempBmp)

        Grf.DrawString(Str, Fnt, Brushes.Black, 0, 0)

        Me.BackgroundImage = TempBmp

        Img.Save("d:\tst.bmp", Imaging.ImageFormat.Bmp) 'Bmp or any another images format
Posted
Comments
[no name] 8-Aug-12 14:29pm
   
"it is giving error"... is not very helpful.
   
What is "Vb/net"? If you don't know even the name of your language...
--SA

Kryukov is correct in that the variable Img doesn't have anything set to it, so of course it won't save anything properly. You can save the file with the bitmap object that you have created. Here is a method that I found and tweeked for one of my apps to save a jpg file:

VB
Private Sub CreateImage(ByVal strText As String, ByVal strFileName As String, _
                            ByVal inFont As Font, ByVal inForeColor As Color, _
                            ByVal inBackColor As Color)
        'Create a bitmap of the required size
        Dim Height As Integer = 400
        Dim Width As Integer = 620
        Dim objBitmap As New Bitmap(Width, Height)

        'Create a Graphics object using this Bitmap object
        Dim objGraphics As Graphics = Graphics.FromImage(objBitmap)

        'RectangleF object defines where the text will be displayed in the specified area of the image
        Dim objRect As New RectangleF(5, 5, 610, 390)

        'Create two SolidBrush type objects
        Dim objBrushForeColor As New SolidBrush(inForeColor)
        Dim objBrushBackColor As New SolidBrush(inBackColor)

        'Draw rectangle using Graphics object and fill it with BackColor
        objGraphics.FillRectangle(objBrushBackColor, 0, 0, Width, Height)

        'Draw Text string on the specified rectangle using Graphics object.
        objGraphics.DrawString(strText, inFont, objBrushForeColor, objRect)

        'Save the image on your hard drive in the specified format in JPEG format
        objBitmap.Save(strFileName & ".JPG", ImageFormat.Jpeg)
        objBrushForeColor.Dispose()
        objBrushBackColor.Dispose()
        objBitmap.Dispose()
        objGraphics.Dispose()
End Sub


My method is hard set to create a 400 x 620 pixel image and I save as a jpg file which was required for my project, but at least this shows you how the save is supposed to work. You should be able to use this code to at least get you past this issue. Hope it helps.
   
Comments
Rahul K Singh 8-Aug-12 23:32pm
   
It worked for me... Thanks...
This is not nice of you: report an error and tell not a word about it. You did not even mention if that is a compiler message or exception. Do you like to waste people's time?

I can see some problems:

The variable Img is not initialized; you just declare it and later never touch it, and then try to call Save. It has nothing to do with your attempt to draw on TempBmp. By the way, check it up: aren't you trying to draw black on black? Such text won't be very well readable… :-)

Besides, there are no cases when hard-coded path names like your "d:\tst.bmp" can be useful. In your case, "d:" may or may not exist (on one of my systems, even "C:" does not exist; this is not a must), and on Windows 7 it might be not even legal. All path names should be calculated during run time, based on different data.

—SA
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900