picturebox控件资源释放问题?

closey 2008-09-10 09:06:32
MyImage 定义为外部变量
MyImage = new Bitmap(fileName);

定时器中每次重新装载图像
if (this.PictureBox.Image != null)
{
this.PictureBox.Image.Dispose();
this.PictureBox.Image = null;
}
this.ctrlPictureBox.Image = MyImage;

编译时,提示参数错误

修改为
if (this.PictureBox.Image != null)
{
this.PictureBox.Image.Dispose();
this.PictureBox.Image = null;
}
this.PictureBox.Image = (Image)MyImage.Clone();

图像显示时闪烁

修改为
if (this.PictureBox.Image != null)
{
this.PictureBox.Image.Dispose();
}
this.PictureBox.Image = (Image)MyImage.Clone();
内存递增

各位老大看看,解释一下。谢谢了。


...全文
345 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
closey 2008-09-10
  • 打赏
  • 举报
回复
为什么4楼的内容运行时出现异常啊。
flon 2008-09-10
  • 打赏
  • 举报
回复
MyImage = new Bitmap(filename);
你不断的把它实例化,内存肯定会增加啊
closey 2008-09-10
  • 打赏
  • 举报
回复
修改为如下,运行正常,但是内存递增
void timer_tick()
{
if (this.PictureBox.Image != null)
{
this.PictureBox.Image.Dispose();
this.PictureBox.Image = null;
}
MyImage = new Bitmap(filename);
this.ctrlPictureBox.Image = MyImage;

}
closey 2008-09-10
  • 打赏
  • 举报
回复
MyImage 定义为外部变量
MyImage = new Bitmap(fileName);
以上部分在初始化部分赋值

在定时器中实现图片的更换。
void timer_tick()
{
if (this.PictureBox.Image != null)
{
this.PictureBox.Image.Dispose();
this.PictureBox.Image = null;
}
this.ctrlPictureBox.Image = MyImage;

}
程序运行时提示参数异常。
closey 2008-09-10
  • 打赏
  • 举报
回复
直接的赋值为什么编译提参数错误呢?
brallow 2008-09-10
  • 打赏
  • 举报
回复
MyImage 定义为外部变量
MyImage = new Bitmap(fileName);

定时器中每次重新装载图像
if (this.PictureBox.Image != null)
{
this.PictureBox.Image.Dispose();
this.PictureBox.Image = null;
}
this.ctrlPictureBox.Image = MyImage;

编译时,提示参数错误
什么错误?这段代码应该是可以的,用类似的代码我是可以编译成功的。
brallow 2008-09-10
  • 打赏
  • 举报
回复
你Clone就相当于把这个对象复制了一次,当然内存会递增了。

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧