老师布置一个实验报告叫什么叠加分析要求用C#实现,然后什么也没说什么也不讲,真的是一脸懵逼,上网查了一下后发现全都是arcgis的,但是好在还是查到了一点有用的思路,如下:
姑且就当它是二选一吧,反正她什么都没说,那也别怪我们自由发挥了,上面两个一看就知道肯定是栅格叠加更简单,所以就搞它吧,再给它替换成图片,来一波狸猫换太子,面子上过得去,显得复杂一点,还不够的话再给她来个进度条动态显示,嘿嘿,完美。
操作步骤:
1.显示栅格
2.显示数据
3.调进度条
4.栅格叠加->调进度条->栅格叠加->调进度条......
另外栅格叠加是button1、显示栅格是button2、显示数据是button3
图片框从左到右1、2、3
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//获取8位二进制码
public string getEightBinary(string binary)
{
//十进制转二进制字符串
// string str = Convert.ToString(row, 2);
//输出: 1010
string strBinary = Convert.ToString(int.Parse(binary), 2);
//保证是8位的二进制数,位数不够用0占位
int len = 8 - strBinary.Length;
string prefix = "";
for (int i = 0; i < len; i++)
{
prefix += "0";
}
strBinary = prefix + strBinary;
return strBinary;
}
//获取莫顿码,输入row,col范围0-255,输出莫顿码范围0-25535
public int getMorton(string row, string col)
{
if (int.Parse(row) > 255 || int.Parse(col) > 255)
{
return -1;
}
//转化为8位二进制码
string x = getEightBinary(row);
string y = getEightBinary(col);
//生成二进制的M码
string strm = "";
//交叉算法,将两个8位二进制码合并位16位莫顿码
for (int i = 0; i < x.Length; i++)
{
strm = strm + x.Substring(i, 1) + y.Substring(i, 1);
}
//将16为2进制码转化为整形数字,即为莫顿码
int m = Convert.ToInt32(strm, 2);
return m;
}
public void creatSange1()
{
Bitmap map = new Bitmap(256, 256);//初始化大小
Graphics g = Graphics.FromImage(map);
Pen pen = new Pen(Color.Black, 1);
for (int i = 0; i < 256; i++)
{
for (int j = 0; j < 256; j++)
{
int m = (i + j) / 2;
if (m < 32)
map.SetPixel(i, j, Color.Red);
else if (m < 64)
map.SetPixel(i, j, Color.Orange);
else if (m < 96)
map.SetPixel(i, j, Color.Yellow);
else if (m < 128)
map.SetPixel(i, j, Color.Green);
else if (m < 160)
map.SetPixel(i, j, Color.Cyan);
else if (m < 192)
map.SetPixel(i, j, Color.Blue);
else if (m < 224)
map.SetPixel(i, j, Color.Purple);
else
map.SetPixel(i, j, Color.Black);
}
}
for (int i = 0; i < 256; i++)
{
for (int j = 0; j < 256; j++)
{
if (i % 32 == 0)
{
g.DrawLine(pen, i, 0, i, 255); //在画布上画一条直线
g.DrawLine(pen, 0, i, 255, i); //在画布上画一条直线
}
}
}
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置图片质量
g.DrawImage(map, 0, 0, 256, 256);//设置位置
map.Save("c:\\data\\" + "栅格1.jpg", ImageFormat.Jpeg);//自己创建一个文件夹,放入生成的图片(根目录下)
}
public void creatSange2()
{
Bitmap map = new Bitmap(256, 256);//初始化大小
Graphics g = Graphics.FromImage(map);
Pen pen = new Pen(Color.Purple, 1);
var source = Graphics.FromImage(map);//存入画布
for (int i = 0; i < 256; i+=32)
{
for (int j = 0; j < 256; j+=32)
{
int m = getMorton(Convert.ToString(i), Convert.ToString(j));
if (m < 6465)
source.DrawString("A", new Font("宋体", 20, FontStyle.Regular), new SolidBrush(Color.White), i, j);//在画布绘制字体
else if (m < 14765)
source.DrawString("B", new Font("宋体", 20, FontStyle.Regular), new SolidBrush(Color.White), i, j);//在画布绘制字体
else if (m < 26451)
source.DrawString("C", new Font("宋体", 20, FontStyle.Regular), new SolidBrush(Color.White), i, j);//在画布绘制字体
else if (m < 36587)
source.DrawString("D", new Font("宋体", 20, FontStyle.Regular), new SolidBrush(Color.White), i, j);//在画布绘制字体
else if (m < 47596)
source.DrawString("E", new Font("宋体", 20, FontStyle.Regular), new SolidBrush(Color.White), i, j);//在画布绘制字体
else if (m < 52306)
source.DrawString("F", new Font("宋体", 20, FontStyle.Regular), new SolidBrush(Color.White), i, j);//在画布绘制字体
else if (m < 60897)
source.DrawString("G", new Font("宋体", 20, FontStyle.Regular), new SolidBrush(Color.White), i, j);//在画布绘制字体
else
source.DrawString("H", new Font("宋体", 20, FontStyle.Regular), new SolidBrush(Color.White), i, j);//在画布绘制字体
}
}
for (int i = 0; i < 256; i++)
{
for (int j = 0; j < 256; j++)
{
if (i % 32 == 0)
{
g.DrawLine(pen, i, 0, i, 255); //在画布上画一条直线
g.DrawLine(pen, 0, i, 255, i); //在画布上画一条直线
}
}
}
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置图片质量
g.DrawImage(map, 0, 0, 256, 256);//设置位置
map.Save("c:\\data\\" + "栅格2.jpg", ImageFormat.Jpeg);//自己创建一个文件夹,放入生成的图片(根目录下)
}
public void combin()
{
var img1 = new Bitmap("c:\\data\\栅格1.jpg");
var img2 = new Bitmap("c:\\data\\栅格2.jpg");
for(int i=5; i<256; i++)
{
for(int j = 0; j < 256; j++)
{
if(img2.GetPixel(i, j).ToArgb() > -(trackBar1.Value*1000000))
{
img1.SetPixel(i, j, Color.White);
}
}
}
Graphics g = Graphics.FromImage(img1);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置图片质量
g.DrawImage(img1, 0, 0, 256, 256);//设置位置
img1.Save("c:\\data\\" + "合并栅格.jpg", ImageFormat.Jpeg);//自己创建一个文件夹,放入生成的图片(根目录下)
}
private void button1_Click(object sender, EventArgs e)
{
combin();
label2.Text = "=";
pictureBox3.ImageLocation = "c:\\data\\合并栅格.jpg";
}
private void button2_Click(object sender, EventArgs e)
{
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
}
private void button2_Click_1(object sender, EventArgs e)
{
creatSange1();
pictureBox1.ImageLocation = "c:\\data\\栅格1.jpg";
}
private void button3_Click(object sender, EventArgs e)
{
label1.Text = "+";
creatSange2();
pictureBox2.ImageLocation = "c:\\data\\栅格2.jpg";
}
}
}