1. 程式人生 > >球形圖生成全景羅盤圖 .NET,KRpano

球形圖生成全景羅盤圖 .NET,KRpano

球形圖

球形圖

生成後的圖

羅盤圖

1、準備一張球形圖 圖片
2、將球形圖繪製成羅盤圖

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;

namespace App
{
    // 生成羅盤圖
    public class Generate
    {
        public Bitmap img = null;

        public Generate()
        {
        }

        ///
<summary>
/// 繪製羅盤圖 /// </summary> /// <param name="oldim">球形圖</param> /// <param name="width">寬度</param> public void GraphicsImg(string oldim,int width = 400) { Bitmap oldimg = new Bitmap(oldim); GenerateBgImg(width, width); var
r = width * 0.5; int oldheight = oldimg.Height; int oldwidth = oldimg.Width; for (int y = 0; y < width; y++){ for (int x = 0; x < width; x++){ var px = x / width; var d = (x - r + 0.5) * (x - r + 0.5) + (y - r + 0.5
) * (y - r + 0.5); d = System.Math.Sqrt(d); var dx = System.Math.Abs(d - r); if (d < r){ var BY = System.Math.Floor(oldheight * (1 - d / r)); var vx = x + 0.5 - r; var vy = (y + 0.5 - r); var vl = System.Math.Sqrt(vx * vx + vy * vy); var v = (System.Math.Acos(vx / vl)); if (vy > 0){ v = v + System.Math.PI; } v = v * 180 / System.Math.PI; v = v - 90; if (v < 0){ v = v + 360; } var yp = v / 360; var BX = System.Math.Floor(oldwidth * yp); if (vy > 0){ BX = oldwidth - BX; } //獲取畫素的RGB顏色值 Color srcColor = oldimg.GetPixel(Convert.ToInt32(BX), Convert.ToInt32(BY)); img.SetPixel(x, y, srcColor); } } } } /// <summary> /// 儲存 /// </summary> public void save() { img.Save("d:/abc.png", System.Drawing.Imaging.ImageFormat.Png); } /// <summary> /// 建立透明背景圖 /// </summary> /// <param name="width">寬度</param> /// <param name="height">高度</param> public void GenerateBgImg(int width, int height) { img = new Bitmap(width, height); Graphics g1 = Graphics.FromImage(img); Color Yourcolor = Color.FromArgb(0, 255, 255, 255);// 透明 Brush brush = new SolidBrush(Yourcolor); g1.FillRectangle(brush, new Rectangle(0, 0, width,height)); } } }