当前位置:编程学习 > C#/ASP.NET >>

出现了outofmemoryexception 异常,是个什么异常,怎么解决?

做画图程序时,若画图次数太多就出现了outofmemoryexception 异常,是个什么异常,怎么解决? --------------------编程问答-------------------- 内存溢出,你是不是有非托管资源没有及时释放?? --------------------编程问答-------------------- 参考
http://www.cnblogs.com/kwklover/archive/2005/05/05/149787.html --------------------编程问答-------------------- 我发现是我画图时使用了 

 bmp=new Bitmap(this.pictureBox1.Width,this.pictureBox1.Height);
this.pictureBox1.Image=(Image)bmp;
g=Graphics.FromImage(this.pictureBox1.Image);
而产生的,我去掉后就不会产生这个异常了,但我也画不了图了,怎么解决啊??? --------------------编程问答-------------------- 对象bmp跟g,有没有实例化? --------------------编程问答-------------------- 你把画图方法中的代码贴出来 --------------------编程问答-------------------- 把资源及时释放啊 --------------------编程问答-------------------- 如何释放 --------------------编程问答--------------------
bmp=new   Bitmap(this.pictureBox1.Width,this.pictureBox1.Height); 
this.pictureBox1.Image=(Image)bmp; 
g=Graphics.FromImage(this.pictureBox1.Image); 


try changing to:


if( this.pictureBox1.Image == null )
{
  this.pictureBox1.Image = new Bitmap(this.pictureBox1.Width,this.pictureBox1.Height); 
}
g=Graphics.FromImage(this.pictureBox1.Image); 
--------------------编程问答-------------------- Dispose()方法看看有没有,及时释放下 --------------------编程问答-------------------- 内存溢出

引用 8 楼 gomoku 的回复:
bmp=new  Bitmap(this.pictureBox1.Width,this.pictureBox1.Height); 
this.pictureBox1.Image=(Image)bmp; 
g=Graphics.FromImage(this.pictureBox1.Image); 


try changing to: 


C# code
if( this.pictureBox1.Image == null )
{
  this.pictureBox1.Image = new Bitmap(this.pictureBox1.Width,this.pictureBox1.Height); 
}
g=Graphics.FromImage(this.pictureBox1.Image); 
--------------------编程问答-------------------- 不是合法的图片文件就会报内存溢出,这是微软的bug --------------------编程问答-------------------- 内存溢出 --------------------编程问答-------------------- 内存溢出 --------------------编程问答-------------------- using
{
 bmp=new   Bitmap(this.pictureBox1.Width,this.pictureBox1.Height); 
this.pictureBox1.Image=(Image)bmp; 
g=Graphics.FromImage(this.pictureBox1.Image); 
}


if(bmp!=null&&!bmp.Disposed)
{
bmp.Dispose();
}
 bmp=new   Bitmap(this.pictureBox1.Width,this.pictureBox1.Height); 
this.pictureBox1.Image=(Image)bmp; 
g=Graphics.FromImage(this.pictureBox1.Image);  --------------------编程问答-------------------- using
{
 bmp=new   Bitmap(this.pictureBox1.Width,this.pictureBox1.Height); 
this.pictureBox1.Image=(Image)bmp; 
g=Graphics.FromImage(this.pictureBox1.Image); 
}


if(bmp!=null&&!bmp.Disposed)
{
bmp.Dispose();
}
 bmp=new   Bitmap(this.pictureBox1.Width,this.pictureBox1.Height); 
this.pictureBox1.Image=(Image)bmp; 
g=Graphics.FromImage(this.pictureBox1.Image);  --------------------编程问答-------------------- using
{
 bmp=new   Bitmap(this.pictureBox1.Width,this.pictureBox1.Height); 
this.pictureBox1.Image=(Image)bmp; 
g=Graphics.FromImage(this.pictureBox1.Image); 
}


if(bmp!=null&&!bmp.Disposed)
{
bmp.Dispose();
}
 bmp=new   Bitmap(this.pictureBox1.Width,this.pictureBox1.Height); 
this.pictureBox1.Image=(Image)bmp; 
g=Graphics.FromImage(this.pictureBox1.Image); 
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,