So continuing work on the moving map GPSd application (yes, I’m still banging on about this).
Previously, I was limited to displaying my ship/plane icon at fixed 45 degree angles, i.e North, North-East, West etc. Although this works really nicely, its not quite as nice as being able to display an image at an arbitrary angle. So I guess I could have 360 different pictures, but what I really needed was a nice way to display a pushpin on the Virtual Earth map at any angle.
Furthermore, I needed to preserve transparency so that icon didn’t obscure the background. Reading around the web, this seems to be something that lots of people have tried to-do, so I thought I would post the source of my asp.net image rotate code.
Here’s how to use it fist of all in HTML.
Here, is the full source of imagerotate.aspx (obviously there is nothing in the page body, just this code behind). Please note that the transparency colour is set to WHITE, but change as you require.
public partial class ImageDisplay : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
int angle = 0;
string image = "";
angle = Convert.ToInt32(Request["angle"].ToString());
image = Request["image"].ToString();
string imagepath = Server.MapPath(image);
Bitmap b = new Bitmap(imagepath);
Bitmap rotated = rotateImage(b, angle);
using (MemoryStream stream = new MemoryStream())
Response.ContentType = "image/png";
private Bitmap rotateImage(Bitmap b, float angle)
Bitmap returnBitmap = new Bitmap(b.Width, b.Height,PixelFormat.Format32bppPArgb);
Graphics g = Graphics.FromImage(returnBitmap);
g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);
g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
g.DrawImage(b, new Point(0, 0));
Technorati Tags: Virtual Earth