need help converting this short[] into a grayscale bmp
Current Error : http://grabilla.com/04c0f-dcadafc9-1274-4cbf-a3a9-dc47d5148c25.html#
the tdata array is a short[] or list< int16>its short grayscale height map
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);
}
}
}
https://social.msdn.microsoft.com/Forums/vstudio/en-US/10252c05-c4b6-49dc-b2a3-4c1396e2c3ab/writing-a-16bit-grayscale-image?forum=csharpgeneral
finally found the right search terms after a few hours of searching :)
ok so the above link got me thinking about the following stuff.
the end solution was this
sources: https://social.msdn.microsoft.com/Forums/vstudio/en-US/1a0919e9-95df-4479-95b8-103e1914e08e/problems-saving-a-12bpp-short-array-as-a-grayscale-bmp?forum=csharpgeneral