【问题标题】:represent array of byte in datagridview表示 datagridview 中的字节数组
【发布时间】:2013-12-26 06:40:47
【问题描述】:

我在 C# 中有一个字节数组:

 byte[] bmp = File.ReadAllBytes("D:\\x.bmp");

我想在DataGridView 中表示如下(三列 - 第一列偏移量,第二列 sizebyte,第三列描述,行是来自x.bmp 的字节数组的元素):

0          2               signature
2          4                size BMP
6          2                reserved
8          2                reserved
10         4                offset start image
14         4                must 40
18         4                 width
22         4                 height
26         2                 must 1

我可以在 C# 中使用DataGridView 以这种方式表示字节数组吗?

【问题讨论】:

    标签: c# arrays datagridview bytearray bmp


    【解决方案1】:

    这段代码怎么样?

    您不需要读取 BMP 文件的所有字节。

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            UpdateDataGridView();
        }
    
        private void UpdateDataGridView()
        {
            byte[] bmp = new byte[28];
            List<BMPInfo> InfoList = new List<BMPInfo>();
    
            using (var fs = new FileStream("D:\\x.bmp", FileMode.Open, FileAccess.Read))
            {
                fs.Read(bmp, 0, bmp.Length);
            }
    
            InfoList.Add(new BMPInfo
            {
                Offset = BitConverter.ToInt32(bmp, 10),
                Size = BitConverter.ToInt32(bmp, 2),
                Description = "Something"
            });
            dataGridView1.DataSource = InfoList;
        }
    }
    
    public class BMPInfo
    {
        public long Offset { get; set; }
        public long Size { get; set; }
        public string Description { get; set;}
    }
    

    【讨论】:

      【解决方案2】:

      我不是winform 人,所以我不知道如何使用DataGridView。 但我以某种方式能够做到这一点:

      这只是给你一个想法。

      private void Form1_Load(object sender, EventArgs e)
              {
                  byte[] offset = { 0, 2, 6, 8, 10, 14, 18, 22, 26 };
                  byte[] sizebyte = { 4, 2, 2, 4, 4, 4, 4, 2 };
                  string[] description = { "signature", "size BMP", "reserved", "reserved", "offset start image", "must 40", "width", "height" };
      
                  this.dataGridView1.Columns.Add("offset", "offset");
                  this.dataGridView1.Columns.Add("sizebyte", "sizebyte");
                  this.dataGridView1.Columns.Add("description", "description");
                  int i = offset.Length;
      
      
                      for (int j = 0; j < i; j++)
                      {
                          DataGridViewRow row = new DataGridViewRow();
                          row.CreateCells(dataGridView1);
                          row.Cells[0].Value = offset.GetValue(j).ToString();
                          if(j<sizebyte.Length)
                          row.Cells[1].Value = sizebyte.GetValue(j).ToString();
                          if (j < description.Count())
                          row.Cells[2].Value = description.GetValue(j).ToString();
                          dataGridView1.Rows.Add(row);
                      }
      
      
      
              }
      

      我知道代码并不完美。但我可以用这个填充我的 dataGridView。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-01-12
        • 2016-03-01
        • 1970-01-01
        • 2012-03-25
        • 2012-04-16
        • 2014-10-13
        • 2011-02-21
        相关资源
        最近更新 更多