1

需要帮助将此短 [] 转换为灰度 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);
    }


}


}
4

1 回答 1

0

https://social.msdn.microsoft.com/Forums/vstudio/en-US/10252c05-c4b6-49dc-b2a3-4c1396e2c3ab/writing-a-16bit-grayscale-image?forum=csharpgeneral

经过几个小时的搜索,终于找到了正确的搜索词:)

好的,所以上面的链接让我想到了以下内容。

最终的解决方案是这样

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

来源: 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

于 2014-12-15T02:10:58.530 回答