球形图生成全景罗盘图 .NET,KRpano
程序员文章站
2022-05-28 12:10:05
...
球形图
生成后的图
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));
}
}
}
上一篇: Python多元线性回归
下一篇: Lua 脚本