大家看看我的代码哪里有错误啊!!! 现在是这段代码在另一个窗体还用 但是到这个就不好用 报未将对想引用到实例的错误,,先说明我断点测试过 img不是空的
public ImageBig(byte[] img)
{
//把二进制数据组装入流
if (img != null)
{
using (MemoryStream ms = new MemoryStream(img))
{
//从数据流组成图片
try
{
this.pictureBox1.Image = Image.FromStream(ms);
}
catch
{ MessageBox.Show("读取图片失败!");
} ms.Close();
ms.Dispose();
} }
}
public ImageBig(byte[] img)
{
//把二进制数据组装入流
if (img != null)
{
using (MemoryStream ms = new MemoryStream(img))
{
//从数据流组成图片
try
{
this.pictureBox1.Image = Image.FromStream(ms);
}
catch
{ MessageBox.Show("读取图片失败!");
} ms.Close();
ms.Dispose();
} }
}
解决方案 »
- 这种数据包怎么模拟发送
- C#中如何扫描磁盘文件,得到某个目录下的所有文本文件
- 大侠们svn在哪下载啊
- 字符串连接
- Programming C# 4th edition chinese o'reilly的那本书,谁有?我实在是找不到了....
- 怎么引用命名空间才能使用PhysicalApplicationPath属性?
- <%@ Register TagPrefix="uc1" TagName="menu" Src="menu.ascx" %>的参数传递问题
- xp风格的mdi窗体,在线等,顶者有分
- 求个 Infragistics 2005 vol 3 for clr2.0的注册码
- C# web开发中,存储过程连接超时
- 求正则表达式,数字区间
- C# 读取网页出现意大急求解码!!!在线等!!
{
[STAThread]
static void Main(string[] args)
{
OpenFileDialog dlg = new OpenFileDialog ();
dlg.Filter="*.jpg|*.jpg||";
dlg.ShowDialog();
new Program().ImageBig(File.ReadAllBytes(dlg.FileName));
Console.ReadKey();
} private PictureBox pictureBox1 = new PictureBox();
public void ImageBig(byte[] img)
{
//把二进制数据组装入流
if (img != null)
{
using (MemoryStream ms = new MemoryStream(img))
{
//从数据流组成图片
try
{
this.pictureBox1.Image = Image.FromStream(ms);
}
catch
{ MessageBox.Show("读取图片失败!");
} ms.Close();
ms.Dispose();
} }
}
}运行很正常。
这句报异常 未将对象引用到实例
new Program().ImageBig(File.ReadAllBytes(dlg.FileName));
你new了这个类以后pictureBox1就不是你调用的类里的那个了
ms.Dispose();删掉再试试,可能是多了这两个东西吧。用了using ,ms会自动关闭,不需要再加上这两个
另外断点看下ms是不是null