需要帮助将此短 [] 转换为灰度 bmp
当前错误:http :
//grabilla.com/04c0f-dcadafc9-1274-4cbf-a3a9-dc47d5148c25.html# tdata 数组是短 [] 或列表 < int16>its短灰度高度图
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Windows.Media.Imaging;
using System.Drawing.Imaging;
namespace ConvertSHTtobmp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int h, w;
h = 1536;
w = 1536;
List<byte> tdata = new List<byte>();
using (BinaryReader br = new BinaryReader(File.Open("C:\\Users\\Keith\\Desktop\\StartZone_1536_1536_0.sht", FileMode.Open)))
{
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
byte temp = br.ReadByte();
tdata.Add (temp);
temp = br.ReadByte();
tdata.Add(temp);
}
}
CreateBitmapFromBytes(tdata.ToArray(), w, h);
}
}
private static void CreateBitmapFromBytes(byte[] pixelValues, int width, int height)
{
//Create an image that will hold the image data
Bitmap pic = new Bitmap(width, height, PixelFormat.Format16bppGrayScale);
//Get a reference to the images pixel data
Rectangle dimension = new Rectangle(0, 0, pic.Width, pic.Height);
BitmapData picData = pic.LockBits(dimension, ImageLockMode.ReadWrite, pic.PixelFormat);
IntPtr pixelStartAddress = picData.Scan0;
//Copy the pixel data into the bitmap structure
System.Runtime.InteropServices.Marshal.Copy(pixelValues, 0, pixelStartAddress, pixelValues.Length);
pic.UnlockBits(picData);
pic.Save(@"C:\Users\Keith\Desktop\heightmap.bmp", ImageFormat.Bmp);
}
}
}