日期:2014-05-17  浏览次数:20909 次

急求赐教,麻烦各位看看,谢谢
如下代码,实现读入十六进制数据,显示灰度图像。
问题是:运行后为何图像不显示,问题在哪里?请赐教!谢谢~~
PS:程序是从朋友那拷过来的,我只改了文件位置和名称(C:\Documents and Settings\Administrator\My Documents\nei.dat),然后根据程序添加了pictureBox和 Timer控件。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication5
{
  public partial class Form1 : Form
  {
  int m = 0;
  FileStream s = new FileStream(@"C:\Documents and Settings\Administrator\My Documents\nei.dat", FileMode.Open);
  public Form1()
  {
  InitializeComponent();
  }

  private void Form1_Load(object sender, EventArgs e)
  {

  }

  public void ShowPic()
  {
   
  byte[] buffer = new byte[54];

  for (int b = 0; b < 54; b++)
  {
  buffer[b] = 0;
  }

  buffer[0] = 66;
  buffer[1] = 77;
  buffer[2] = 54;
  buffer[3] = 4;
  buffer[4] = 1;
  buffer[5] = 0;
  buffer[10] = 54;
  buffer[11] = 4;
  buffer[14] = 40;
  buffer[18] = 0;
  buffer[19] = 1;
  buffer[22] = 0;
  buffer[23] = 1;
  buffer[26] = 1;
  buffer[28] = 8;
  buffer[36] = 1;
  buffer[38] = 116;
  buffer[39] = 18;
  buffer[42] = 116;
  buffer[43] = 18;
  buffer[47] = 1;
  buffer[51] = 1;

  byte[] buffer1 = new byte[4 * 256];
  for (int k = 0; k < 256; k++)
  {
  buffer1[4 * k] = (byte)k;
  buffer1[4 * k + 1] = (byte)k;
  buffer1[4 * k + 2] = (byte)k;
  buffer1[4 * k + 3] = 0;
  }
  //读入dat文件内容

  byte[] byData = new byte[65536 * 2];
  byte[] byData1 = new byte[65536 * 2];
  byte[] byData2 = new byte[65536 * 2];
  int length = byData.GetLength(0);
   
  s.Read(byData, 0, 65536 * 2);
  for (int i = 0; i < 65536; i++)
  {
  byData[i] = (byte)(((int)byData[2 * i] + (int)byData[2 * i + 1] * 256) / 4);
  //图像数据左移八位然后右移两位实现如:A1 02到02 A1的转换  
  }
  m++;
  if (m >= 301)
  {
  s.Seek(0,SeekOrigin.Begin);
  m = 0;  
  }
  for (int i = 0; i < 65536; i++)
  {
  byData1[i] = byData[65535 - i];
  }
  for (int k = 0; k < 256; k++)
  {
  for (int j = 0; j < 256; j++)
  { byData2[j + k * 256] = byData1[(255 - j) + k * 256]; }
  }
  MemoryStream stream = new MemoryStream(length + 54+ 256*4);//为头腾出54个长度的空间
  stream.Write(buffer, 0, 54)