下面的这段代码要实现的功能为读取.dat文件并转化为图像,在PictureBox中画出图像。现在的问题是:1.已经添加using System.Drawing,但报错“System.EventArgs”不包含“Graphics”的定义,并且找不到可接受类型为“System.EventArgs”的第一个参数的扩展方法“Graphics”(是否缺少 using 指令或程序集引用?);2.没有添加PictureBox按钮时能正常读取数据,但是显示不出图像。求各位前辈指点一下!
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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private ushort pixel;
private byte pix;
private int lngWidthIrImage = 0;
private int lngHeightIrImage = 0;
private Bitmap bmp = null;
private List<float> maxList;
private List<float> minList;
private int lngMinPixelValue;
private int lngMaxPixelValue;
private int lngSpan;
public Form1()
{
InitializeComponent();
}
private void 打开DATtoolStripMenuItem1Click(object sender, EventArgs e)
{
try
{ List<float[,]> mytemperature = new List<float[,]>();
// if (!FileManager.OpenDatFile(mySeqData))
if (!OpenDatFileByByte(mytemperature))
{
MessageBox.Show("读取失败");
mytemperature = null;
}
else
{
MessageBox.Show("读取成功"); Float2Bitmap(mytemperature[1], bmp);
//Graphics g = this.CreateGraphics();
//g.DrawImage(bmp,240,320);
}
}
catch (Exception ex)
{ }
} private void 显示图像ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
//Float2Bitmap(mytemperature[1], bmp);
PictureBox.Image = System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());
Graphics g = e.Graphics;
Pen p = new Pen(Color.Red, 2);
g.DrawLine(p, 0, 0, 100, 100);
}
catch (Exception ex)
{ }
}
public static bool OpenDatFileByByte(List<float[,]>mytemperature)
{
OpenFileDialog openFileDlg = new OpenFileDialog();
openFileDlg.Filter = "*.dat|*.DAT|*.*|*.*";
openFileDlg.Title = "选择序列文件";
openFileDlg.Multiselect = false;
string fileName;
if (openFileDlg.ShowDialog() != DialogResult.OK)
{
return false;
}
fileName = openFileDlg.FileName;
using (FileStream s = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[s.Length];
s.Read(buffer, 0, buffer.Length);
if (ReadBinaryBuffer(buffer,mytemperature))
{
//mytemperature.FileName = fileName;
return true;
}
else
{
return false;
}
}
}
/////
private static bool ReadBinaryBuffer(byte[] buffer, List<float[,]> mytemperature)
{
int width=320;
int height=240; if (!ReadBinaryBufferByInt16(height, width, sizeof(Int16), buffer, mytemperature))
{
return false;
}
return true;
//seqData.Width = width;
//seqData.Height = height;
//seqData.Frame = seqData.TemperatureList.Count;
} private static bool ReadBinaryBufferByInt16(int height, int width, int sizeOfType, byte[] buffer, List<float[,]> listFloatArr)
{
listFloatArr.Clear();
int length = width * height;
int frame = buffer.Length / (length * sizeOfType);
int remainder = buffer.Length % (length * sizeOfType);
int pos = 0;
if (0 != remainder)
{
return false;
}
try
{
for (int f = 0; f < frame; f++)
{
float[,] tmpFrame = new float[width, height];
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
tmpFrame[w, h] = ((float)BitConverter.ToInt16(buffer, pos)) / 100.0f;
pos += sizeOfType;
}
}
listFloatArr.Add(tmpFrame);
}
}
catch (ArgumentOutOfRangeException e)
{
return false;
}
return true;
}
protected virtual void Float2Bitmap(float[,] im, Bitmap bmp)
{
// We have an image - cast it to proper type
//获得从相机采集到的图片的长宽 lngWidthIrImage = im.GetLength(0);
lngHeightIrImage = im.GetLength(1); // Find minimum and maximum pixels
float lngMinPixelValue = 65535;
float lngMaxPixelValue = 0;
for (int y = 0; y < lngHeightIrImage; y++)
{
for (int x = 0; x < lngWidthIrImage; x++)
{
float pixel = im[x, y]; if (pixel < lngMinPixelValue)
lngMinPixelValue = pixel;
else if (pixel > lngMaxPixelValue)
lngMaxPixelValue = pixel;
}
} float lngSpan = lngMaxPixelValue - lngMinPixelValue;
maxList.Add(lngMaxPixelValue);
minList.Add(lngMinPixelValue); // Convert 16-bit pixels to color values and fill bitmap
for (int y = 0; y < lngHeightIrImage; y++)
{
for (int x = 0; x < lngWidthIrImage; x++)
{
float pixel = (float)im[x, y];
byte pix = (byte)(((pixel - lngMinPixelValue) * 255) / lngSpan);
bmp.SetPixel(x, y, Color.FromArgb(pix, pix, pix));
}
}
//PictureBox.Image = System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());
} /*
private void Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{ if (mytemperature[1] != null)
{ Graphics g = e.Graphics; g.DrawImage(mytemperature[1], newRectangle(this.AutoScrollPosition.X, this.AutoScrollPosition.Y, (int)(showBitmap.Width), (int)(showBitmap.Height))); } }*/ }
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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private ushort pixel;
private byte pix;
private int lngWidthIrImage = 0;
private int lngHeightIrImage = 0;
private Bitmap bmp = null;
private List<float> maxList;
private List<float> minList;
private int lngMinPixelValue;
private int lngMaxPixelValue;
private int lngSpan;
public Form1()
{
InitializeComponent();
}
private void 打开DATtoolStripMenuItem1Click(object sender, EventArgs e)
{
try
{ List<float[,]> mytemperature = new List<float[,]>();
// if (!FileManager.OpenDatFile(mySeqData))
if (!OpenDatFileByByte(mytemperature))
{
MessageBox.Show("读取失败");
mytemperature = null;
}
else
{
MessageBox.Show("读取成功"); Float2Bitmap(mytemperature[1], bmp);
//Graphics g = this.CreateGraphics();
//g.DrawImage(bmp,240,320);
}
}
catch (Exception ex)
{ }
} private void 显示图像ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
//Float2Bitmap(mytemperature[1], bmp);
PictureBox.Image = System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());
Graphics g = e.Graphics;
Pen p = new Pen(Color.Red, 2);
g.DrawLine(p, 0, 0, 100, 100);
}
catch (Exception ex)
{ }
}
public static bool OpenDatFileByByte(List<float[,]>mytemperature)
{
OpenFileDialog openFileDlg = new OpenFileDialog();
openFileDlg.Filter = "*.dat|*.DAT|*.*|*.*";
openFileDlg.Title = "选择序列文件";
openFileDlg.Multiselect = false;
string fileName;
if (openFileDlg.ShowDialog() != DialogResult.OK)
{
return false;
}
fileName = openFileDlg.FileName;
using (FileStream s = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[s.Length];
s.Read(buffer, 0, buffer.Length);
if (ReadBinaryBuffer(buffer,mytemperature))
{
//mytemperature.FileName = fileName;
return true;
}
else
{
return false;
}
}
}
/////
private static bool ReadBinaryBuffer(byte[] buffer, List<float[,]> mytemperature)
{
int width=320;
int height=240; if (!ReadBinaryBufferByInt16(height, width, sizeof(Int16), buffer, mytemperature))
{
return false;
}
return true;
//seqData.Width = width;
//seqData.Height = height;
//seqData.Frame = seqData.TemperatureList.Count;
} private static bool ReadBinaryBufferByInt16(int height, int width, int sizeOfType, byte[] buffer, List<float[,]> listFloatArr)
{
listFloatArr.Clear();
int length = width * height;
int frame = buffer.Length / (length * sizeOfType);
int remainder = buffer.Length % (length * sizeOfType);
int pos = 0;
if (0 != remainder)
{
return false;
}
try
{
for (int f = 0; f < frame; f++)
{
float[,] tmpFrame = new float[width, height];
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
tmpFrame[w, h] = ((float)BitConverter.ToInt16(buffer, pos)) / 100.0f;
pos += sizeOfType;
}
}
listFloatArr.Add(tmpFrame);
}
}
catch (ArgumentOutOfRangeException e)
{
return false;
}
return true;
}
protected virtual void Float2Bitmap(float[,] im, Bitmap bmp)
{
// We have an image - cast it to proper type
//获得从相机采集到的图片的长宽 lngWidthIrImage = im.GetLength(0);
lngHeightIrImage = im.GetLength(1); // Find minimum and maximum pixels
float lngMinPixelValue = 65535;
float lngMaxPixelValue = 0;
for (int y = 0; y < lngHeightIrImage; y++)
{
for (int x = 0; x < lngWidthIrImage; x++)
{
float pixel = im[x, y]; if (pixel < lngMinPixelValue)
lngMinPixelValue = pixel;
else if (pixel > lngMaxPixelValue)
lngMaxPixelValue = pixel;
}
} float lngSpan = lngMaxPixelValue - lngMinPixelValue;
maxList.Add(lngMaxPixelValue);
minList.Add(lngMinPixelValue); // Convert 16-bit pixels to color values and fill bitmap
for (int y = 0; y < lngHeightIrImage; y++)
{
for (int x = 0; x < lngWidthIrImage; x++)
{
float pixel = (float)im[x, y];
byte pix = (byte)(((pixel - lngMinPixelValue) * 255) / lngSpan);
bmp.SetPixel(x, y, Color.FromArgb(pix, pix, pix));
}
}
//PictureBox.Image = System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());
} /*
private void Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{ if (mytemperature[1] != null)
{ Graphics g = e.Graphics; g.DrawImage(mytemperature[1], newRectangle(this.AutoScrollPosition.X, this.AutoScrollPosition.Y, (int)(showBitmap.Width), (int)(showBitmap.Height))); } }*/ }
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货