ASP.NET and GDI+

The image generator part 2

Okay, now we make like a pen... The drawing function. You can call it what you want, I call it:

Private Function DrawTextToScreen(ByVal Rect As System.Drawing.Rectangle, ByVal Word As String, _
     ByVal FontName As String, ByVal FontSize As Integer, ByVal FontColorMain As Color, _
     ByVal FontSecondColor As Color, ByVal BackColorMain As Color, ByVal BackColorSecond As Color)

         '# Create a path to output the image to, it is temporary
         '# Check your iis and windows folder permissions, your browser have to be able to write
         '# to the folder you specify
         Dim imgPath As String
         imgPath = Server.MapPath("./tmpImages/tmpImg.jpg")

         '# Check to see if a image by that name already exists and delete it if it does
         If File.Exists(imgPath) Then
             Kill(imgPath)
         End If

         '# Create a blank image 200x50 and assign it to a graphics object
         Dim outImage As New Bitmap(200, 50)
         Dim imgWriter As Graphics
         imgWriter = Graphics.FromImage(outImage)

         '# Fill the background with the color specified
         Dim BackgroundBrush As New Drawing2D.LinearGradientBrush(Rect, BackColorMain, BackColorSecond, _
         Drawing2D.LinearGradientMode.Horizontal) '# You can set the type of gradient to whatever you like
         imgWriter.FillRectangle(BackgroundBrush, Rect)

         '# What we actually came here to do... draw the word, and a little transformation :)
         Dim FontBrush As New Drawing2D.LinearGradientBrush(Rect, FontColorMain, FontColorSecond, _
         Drawing2D.LinearGradientMode.Horizontal)
         Dim fFont As New Font(FontName, FontSize, FontStyle.Bold) '# Once again you can make the style and
         '# gradient mode the way you like it, or even make it a parameter...
         
         '# Setting the alignment of the text
         Dim AlignSting As New System.Drawing.StringFormat
         AlignString = StringAlignment.Center

        '# Now this is something i decided to through in extra, rotating the text a bit... you can ignore this part
        '# If you want to, but it gives a good idea of what GDI+ is capable of
        Dim Rotation As Integer
        Rotation = 45 '# 45 Degrees for rotation, a larger word will be written off the screen...
       
       '# Draw the actual text
       imgWriter.RotateTransform(Rotation) '# Rotate the canvas
       imgWriter.DrawString(Word, fFont, FontBrush, 100, 5, salign)'# Draw the text normally
       imgWriter.RotateTransform(-Rotation)'# Reset the canvas rotation, so the text rotates... ;)

       '# Now i'm going to draw a black border, just for the hell of it...
       imgWriter.DrawLine(Drawing.Pens.Black, 0, 0, 200, 0)      '# Drawing the top horizontal line
       imgWriter.DrawLine(Drawing.Pens.Black, 199, 0, 199, 49) '# Drawing righthand vertical
                                                                                           '# Note that the x and y coordinates is one less on
                                                                                           '# Right side of the screen
       imgWriter.DrawLine(Drawing.Pens.Black, 199, 49, 0, 49)   '# Drawing the bottom horizontal line line
       imgWriter.DrawLine(Drawing.Pens.Black, 0, 50, 0, 0)        '# Drawing left vertical line

       '# You can use RotateTransform on any of the drawing objects, just remember to center whatever you are
       '# drawing, otherwise the rotate transform starts to rotate at the 0,0 coordinates of the rectangle you are
       '# drawing to, not the object being drawn.

       '# Save the image and load it into a stream that you pass on to the next function
       outImage.Save(imgPath)
       Dim tmpFileStream As FileStream
       tmpStream = New FileStream(imgPath, FileAccess.ReadWrite, FileAccess.ReadWrite, FileShare.ReadWrite)
       '# Call the function that outputs the actual image data
       DisplayImage(tmpStream)
       '# KKND the image...
       tmpStream.Close()
       Kill(imgPath)
     
End Function

And so the final part, the binary output (forgive me a cruel chuckle) if you have never worked with this type of project, you'll be amazed at what you can do, also appalled at what you can't, and faintly amused how insecure Internet Explorer is. Basically you can write a lot of stuff to the <img> tag and IE just accepts it.

You might also like...

Comments

About the author

Ruan Meyer

Ruan Meyer Saudi Arabia

erm, what can I say... not much, 'cept that I like music, nwn, dnd, programming (C, C++, C#, VB) etc. and Jack Daniels... ;) oh yes and girls...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Linux is only free if your time has no value” - Jamie Zawinski