Click here to Skip to main content
15,898,134 members
Articles / Web Development / ASP.NET

Mapping Images on Spherical Surfaces Using C#

Rate me:
Please Sign up or sign in to vote.
4.94/5 (38 votes)
15 Jul 2016GPL31 min read 164.8K   2.8K   93  
Mapping images on spherical surfaces using C#
<%@ Page Language="c#" Debug="true" Explicit="True" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Web.Mail" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Microsoft.CSharp" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.CodeDom.Compiler" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Import Namespace="System.Collections" %>

<script language="C#" runat="server">

    private void Page_Load(object sender, System.EventArgs e)
    {
        System.Drawing.Image image1 = new Bitmap(Server.MapPath("./images/worldmap4.gif"));
        Bitmap imgBitmap = new Bitmap(image1);

        Bitmap newBitmap = new Bitmap(200, 200, PixelFormat.Format32bppArgb);
        Graphics g = Graphics.FromImage(newBitmap);

        Pen blackPen = new Pen(Color.Black);
        Color clrBackground = Color.Black;
        g.FillRectangle(new SolidBrush(clrBackground), new Rectangle(0, 0, 200, 200));

        Rectangle rect = new Rectangle(0, 0, 200, 200);

        ///////////////////////////////////////
        Graphics graphics = g;

        double radius = 50.0;

        double theta0 = 0.0;
        double theta1 = 2.0*Math.PI;
        double phi0 = 0.0;
        double phi1 =  Math.PI;
        
        for (int i = 0; i < imgBitmap.Width; i++)
        {
            for (int j = 0; j < imgBitmap.Height; j++)
            {
                double theta = Algebra.MapCoordinate(0.0, imgBitmap.Width - 1, theta1, theta0, i);
                double phi = Algebra.MapCoordinate( 0.0, imgBitmap.Height - 1,phi0, phi1, j);

                double x = radius * Math.Sin(phi) * Math.Cos(theta);
                double y = radius * Math.Sin(phi) * Math.Sin(theta);
                double z = radius * Math.Cos(phi);

                Algebra.RotX(1.5, ref y, ref z);
                Algebra.RotY(-2.5, ref x, ref z);

                if (z > 0)
                {
                    Color color = imgBitmap.GetPixel(i, j);
                    Brush brs = new SolidBrush(color);
                    Pen pen = new Pen(color);
                    int ix = (int)x + 100;
                    int iy = (int)y + 100;
                    graphics.FillRectangle(brs, ix, iy, 1, 1);
                }
            }
        }
        
       
        ///////////////////////////////////////
        MemoryStream tempStream = new MemoryStream();
        newBitmap.Save(tempStream, ImageFormat.Png);
        Response.ClearContent();
        Response.ContentType = "image/png";
        Response.BinaryWrite(tempStream.ToArray());
        Response.Flush();

    }


</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Mapping</title>
</head>
<body style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px;
    padding-top: 0px; text-align: center;">
    <form id="form1" runat="server">
    </form>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2275356-1";
urchinTracker();
</script></body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
CEO
Brazil Brazil
"A well written code is self explanatory" - Anonymous Programmer
"The number of meetings is directly proportional to the bad management" - Another Anonymous Programmer
Founder @TIHUNTER.COM.BR
Linkedin Profile

Comments and Discussions