Got an issue converting a short[] into a bitmap which class should I be using?

787 views Asked by At

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);
    }


}


}
1

There are 1 answers

1
Reza On BEST ANSWER

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

using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;
namespace WpfApplication1
{
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();
    }
    private void button1_Click(object sender, RoutedEventArgs e)
    {
      short[] Buf = new short[64 * 64];
      BitmapSource bmpSrc = BitmapSource.Create(
        64, 64, 96, 96, PixelFormats.Gray16, null, Buf, 128);
      TransformedBitmap transformedBmp = new TransformedBitmap(bmpSrc, new RotateTransform(-90));
      PngBitmapEncoder enc = new PngBitmapEncoder();
      enc.Frames.Add(BitmapFrame.Create(transformedBmp));
      using (FileStream fs = new FileStream("D:\\Temp\\Test.png", FileMode.Create))
      {
        enc.Save(fs);
      }
    }
   }
   }    

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