Click here to Skip to main content
13,559,281 members
Click here to Skip to main content
Add your own
alternative version


88 bookmarked
Posted 23 Jul 2007
Licenced GPL3

Mapping Images on Spherical Surfaces Using C#

, 15 Jul 2016
Rate this:
Please Sign up or sign in to vote.
Mapping images on spherical surfaces using C#


This article describes how to map a flat 2D image (JPG, BMP, or GIF) on a sphere by using basic algebra.

The process is very simple where the x axis of the image will be mapped on sphere longitudes and the y axis of the image will be mapped on sphere latitudes.

The process of mapping is similar to proportion equations x-x0/y-y0 = px-x0/py-y0

public static double MapCoordinate(double i1, double i2, double w1,
    double w2, double p)
    return ((p - i1) / (i2 - i1)) * (w2 - w1) + w1;

Screenshot - worldmap4.gif
Original image

Screenshot - mapping.png
Resulting image


A Sphere Can Be Represented by Spherical Coordinates in R3

  • radius
  • phi (latitude angle)
  • theta (longitude angle)

Image 2

  • Where radius is a constant, phi=[-PI/2,PI/2], and theta=[0,2*PI]


To Find the Cartesian Coordinates from Spherical Coordinates

  • x = radius * sin(phi) * cos(theta)
  • y = radius * sin(phi) * sin(theta)
  • z = radius * cos(theta)
double phi0 = 0.0;
double phi1 = Math.PI;
double theta0 = 0.0;
double theta1 = 2.0*Math.PI;

The Code

At first we code the image loading

System.Drawing.Image image1 = new Bitmap(Server.MapPath(
Bitmap imgBitmap = new Bitmap(image1);

Now we make a loop through the 2 dimensions of the image, map phi and theta angles from image coordinates, get the cartesian 3D coordinates from phi and theta, provide some rotation to the obtained 3D points and plot them with respective image color:

for (int i = 0; i < imgBitmap.Width; i++)
     for (int j = 0; j < imgBitmap.Height; j++)
          // map the angles from image coordinates
          double theta = Algebra.MapCoordinate(0.0, imgBitmap.Width - 1,
              theta1, theta0, i);
          double phi = Algebra.MapCoordinate( 0.0, imgBitmap.Height - 1,phi0,
              phi1, j);
          // find the cartesian coordinates
          double x = radius * Math.Sin(phi) * Math.Cos(theta);
          double y = radius * Math.Sin(phi) * Math.Sin(theta);
          double z = radius * Math.Cos(phi);
          // apply rotation around X and Y axis to reposition the sphere
          RotX(1.5, ref y, ref z);
          RotY(-2.5, ref x, ref z);
          // plot only positive points
          if (z > 0)
               Color color = imgBitmap.GetPixel(i, j);
               Brush brs = new SolidBrush(color);
               int ix = (int)x + 100;
               int iy = (int)y + 100;
               graphics.FillRectangle(brs, ix, iy, 1, 1);

The Rotation Functions [almost forgot]

Actually I made a 3D Math class, but here you will need only these functions

public static void RotX(double angle, ref double y, ref double z)
     double y1 = y * System.Math.Cos(angle) - z * System.Math.Sin(angle);
     double z1 = y * System.Math.Sin(angle) + z * System.Math.Cos(angle);
     y = y1;
     z = z1;
public static void RotY(double angle, ref double x, ref double z)
     double x1 = x * System.Math.Cos(angle) - z * System.Math.Sin(angle);
     double z1 = x * System.Math.Sin(angle) + z * System.Math.Cos(angle);
     x = x1;
     z = z1;
public static void RotZ(double angle, ref double x, ref double y)
     double x1 = x * System.Math.Cos(angle) - y * System.Math.Sin(angle);
     double y1 = x * System.Math.Sin(angle) + y * System.Math.Cos(angle);
     x = x1;
     y = y1;

See sample


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Brazil Brazil
Senior Analyst

Linkedin Profile

You may also be interested in...


Comments and Discussions

QuestionHow do you avoid clashes with remapped pixels? Pin
JWhattam11-Nov-15 12:47
memberJWhattam11-Nov-15 12:47 
QuestionCan not find the complete ource code Pin
mans.098724-Jan-13 5:02
membermans.098724-Jan-13 5:02 
GeneralMy vote of 5 Pin
manoj kumar choubey26-Feb-12 21:44
membermanoj kumar choubey26-Feb-12 21:44 
QuestionAny ideas to make it faster? Pin
Tiramisung1-Apr-10 16:16
memberTiramisung1-Apr-10 16:16 
AnswerRe: Any ideas to make it faster? Pin
andalmeida2-Apr-10 3:55
memberandalmeida2-Apr-10 3:55 
GeneralRe: Any ideas to make it faster? Pin
Tiramisung4-Apr-10 22:07
memberTiramisung4-Apr-10 22:07 
GeneralRe: Any ideas to make it faster? Pin
simonxy17-Aug-15 23:55
membersimonxy17-Aug-15 23:55 
AnswerRe: Any ideas to make it faster? Pin
CodeWraith29-Jun-17 2:56
memberCodeWraith29-Jun-17 2:56 
GeneralI really upset! Pin
Mr. Cencious12-Nov-07 23:28
memberMr. Cencious12-Nov-07 23:28 
GeneralRe: I really upset! Pin
andalmeida13-Nov-07 0:56
memberandalmeida13-Nov-07 0:56 
GeneralRe: I really upset! Pin
Mr. Cencious15-Nov-07 16:15
memberMr. Cencious15-Nov-07 16:15 
GeneralRe: I really upset! Pin
Mr. Cencious29-Nov-07 20:41
memberMr. Cencious29-Nov-07 20:41 
GeneralRotation Pin
seeblunt11-Aug-07 17:36
memberseeblunt11-Aug-07 17:36 
GeneralRe: Rotation Pin
seeblunt11-Aug-07 18:02
memberseeblunt11-Aug-07 18:02 
GeneralRe: Rotation Pin
andalmeida12-Aug-07 14:04
memberandalmeida12-Aug-07 14:04 
QuestionMapping an image Cylindrical Surface Pin
zhongyisun10-Aug-07 6:27
memberzhongyisun10-Aug-07 6:27 
AnswerRe: Mapping an image Cylindrical Surface Pin
andalmeida10-Aug-07 7:24
memberandalmeida10-Aug-07 7:24 
GeneralRe: Mapping an image Cylindrical Surface Pin
zhongyisun10-Aug-07 7:45
memberzhongyisun10-Aug-07 7:45 
GeneralCurve tracing algorithm Pin
GD602-Aug-07 12:09
memberGD602-Aug-07 12:09 
GeneralRe: Curve tracing algorithm Pin
andalmeida3-Aug-07 2:50
memberandalmeida3-Aug-07 2:50 
GeneralRe: Curve tracing algorithm Pin
GD605-Aug-07 11:03
memberGD605-Aug-07 11:03 
QuestionWhy? Pin
Paul Selormey23-Jul-07 20:41
memberPaul Selormey23-Jul-07 20:41 
AnswerRe: Why? Pin
andalmeida24-Jul-07 2:39
memberandalmeida24-Jul-07 2:39 
AnswerRe: Why? Pin
andalmeida25-Jul-07 5:57
memberandalmeida25-Jul-07 5:57 
AnswerRe: Why? Pin
Paul Selormey25-Jul-07 6:24
memberPaul Selormey25-Jul-07 6:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180515.1 | Last Updated 15 Jul 2016
Article Copyright 2007 by andalmeida
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid