Rotate an image using GDI+

June 04, 2011 - 13:06

Rotating an image can be usefull when dynamically adding items to an existing image, such as adding a "SOLD" stamp across a products image.

Unfortunatly you cannot add text at an angle to an image directly, so you must rotate the image, then add the text and rotate it back.

To do this we load up the image first into the newimage bitmap and then rotate it.

<%@ Import Namespace="system.drawing" %>

Dim newimage As bitmap
newimage = system.drawing.Image.FromFile(server.mappath("images/theimage.png"))
Dim gr As Graphics = Graphics.FromImage(newimage)
Dim Rotation As Integer
Rotation = 45 '# The angle of rotation.
gr.RotateTransform(Rotation) '# Rotate the canvas


After this step you would use DrawString to write the text onto the canvas, and then finally do the rotation again, but this time with a -45 rotation to return the image to its original angle.



© 2011 simplevb.net