在将short []转换为位图时遇到问题,我应该使用哪个类?

雷扎

需要帮助将此short []转换为灰度bmp
当前错误:http : //grabilla.com/04c0f-dcadafc9-1274-4cbf-a3a9-dc47d5148c25.html# tdata数组是short []或list <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);
    }


}


}
雷扎

https://social.msdn.microsoft.com/Forums/vstudio/zh-CN/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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在将short []转换为位图时遇到问题,我应该使用哪个类?

来自分类Dev

我在将类组件中的功能转换为功能时遇到问题

来自分类Dev

将32位图像转换为黑白黑白图像时遇到问题

来自分类Dev

我在将indexOf转换为for循环时遇到问题,因此我有错误的答案

来自分类Dev

我在将python .pack()转换为.grid()时遇到问题

来自分类Dev

我在将MATLAB代码转换为Python时遇到问题

来自分类Dev

在将黑白转换为PNG时遇到问题

来自分类Dev

将 Python 代码转换为 Java 时遇到问题

来自分类Dev

使用convert将.png批量转换为.pdf时遇到问题

来自分类Dev

使用R将大型栅格转换为多边形时遇到问题

来自分类Dev

使用convert将.png批量转换为.pdf时遇到问题

来自分类Dev

将char转换为short

来自分类Dev

将 char* 转换为 short*

来自分类Dev

使用数组将罗马数字转换为阿拉伯数字时遇到问题

来自分类Dev

如何将const unsigned short转换为unsigned short?

来自分类Dev

将short转换为VariantType(从short提取VariantType)

来自分类Dev

将ElasticSearch多索引查询转换为NEST查询时遇到问题

来自分类Dev

将Pig Latin JavaScript函数转换为GUI时遇到问题

来自分类Dev

将字符串转换为数字时遇到问题

来自分类Dev

将打印队列的迭代函数转换为递归函数时遇到问题

来自分类Dev

将keras / tensorflow h5 / json转换为Tensorflow PB时遇到问题

来自分类Dev

将元组字典转换为有效字典时遇到问题吗?

来自分类Dev

将C#PageAsyncTask()转换为VB.Net等效文件时遇到问题

来自分类Dev

将pinescript v2转换为v3时遇到问题

来自分类Dev

将简单的Objective-C块转换为Swift时遇到问题

来自分类Dev

将QString转换为unsigned short int

来自分类Dev

将char [2]转换为unsigned short时出错?

来自分类Dev

使用if语句将类添加到元素时遇到问题

来自分类Dev

我在将引号放在引号中时遇到问题

Related 相关文章

  1. 1

    在将short []转换为位图时遇到问题,我应该使用哪个类?

  2. 2

    我在将类组件中的功能转换为功能时遇到问题

  3. 3

    将32位图像转换为黑白黑白图像时遇到问题

  4. 4

    我在将indexOf转换为for循环时遇到问题,因此我有错误的答案

  5. 5

    我在将python .pack()转换为.grid()时遇到问题

  6. 6

    我在将MATLAB代码转换为Python时遇到问题

  7. 7

    在将黑白转换为PNG时遇到问题

  8. 8

    将 Python 代码转换为 Java 时遇到问题

  9. 9

    使用convert将.png批量转换为.pdf时遇到问题

  10. 10

    使用R将大型栅格转换为多边形时遇到问题

  11. 11

    使用convert将.png批量转换为.pdf时遇到问题

  12. 12

    将char转换为short

  13. 13

    将 char* 转换为 short*

  14. 14

    使用数组将罗马数字转换为阿拉伯数字时遇到问题

  15. 15

    如何将const unsigned short转换为unsigned short?

  16. 16

    将short转换为VariantType(从short提取VariantType)

  17. 17

    将ElasticSearch多索引查询转换为NEST查询时遇到问题

  18. 18

    将Pig Latin JavaScript函数转换为GUI时遇到问题

  19. 19

    将字符串转换为数字时遇到问题

  20. 20

    将打印队列的迭代函数转换为递归函数时遇到问题

  21. 21

    将keras / tensorflow h5 / json转换为Tensorflow PB时遇到问题

  22. 22

    将元组字典转换为有效字典时遇到问题吗?

  23. 23

    将C#PageAsyncTask()转换为VB.Net等效文件时遇到问题

  24. 24

    将pinescript v2转换为v3时遇到问题

  25. 25

    将简单的Objective-C块转换为Swift时遇到问题

  26. 26

    将QString转换为unsigned short int

  27. 27

    将char [2]转换为unsigned short时出错?

  28. 28

    使用if语句将类添加到元素时遇到问题

  29. 29

    我在将引号放在引号中时遇到问题

热门标签

归档