以下是C#代码,实现了点击按钮1通过open file dialog读取栅格数据,点击按钮2计算其像元值总和的功能:

using System;
using System.Windows.Forms;
using System.IO;

namespace RasterCalculator
{
    public partial class Form1 : Form
    {
        string filePath;
        int sum = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                filePath = openFileDialog1.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(filePath))
            {
                using (BinaryReader reader = new BinaryReader(File.Open(filePath, FileMode.Open)))
                {
                    int fileSize = (int)reader.BaseStream.Length;
                    int byteCount = fileSize / sizeof(Int16);

                    for (int i = 0; i < byteCount; i++)
                    {
                        Int16 value = reader.ReadInt16();
                        sum += value;
                    }

                    label1.Text = "Sum of pixel values: " + sum.ToString();
                }
            }
            else
            {
                MessageBox.Show("Please select a file first.");
            }
        }
    }
}

在此代码中,我们使用了一个OpenFileDialog对象来显示打开文件对话框,让用户选择要读取的栅格数据文件。在用户选择文件后,我们将其路径存储在filePath变量中。

在按钮2的点击事件中,我们首先检查filePath是否为null或空字符串,以确保用户已经选择了一个文件。然后,我们使用BinaryReader类从文件中读取像元值,并将它们累加到sum变量中。最后,我们将计算出的总和显示在label1控件中。

请注意,此代码假定输入文件是16位整数类型的栅格数据。如果您的数据类型不同,请相应地更改代码以正确读取和计算像元值

给出代码实现功能:C#中点击按钮1通过open file dialog读取栅格数据点击按钮2计算其像元值总和

原文地址: http://www.cveoy.top/t/topic/d5o8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录