关于内存的问题:很快把我512M内存耗尽

yangxingbo yangxingbo 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

我写的一屏保程序,每3S从资源里读取变换一张图片。
下面是关于换图片的代码:

每换一第图片,内存就多用4M左右,我一时也想不出,为什么。有时到了内存会自动释放,有时,则
一直到耗尽内存为止。
private void changeFile()
{

Random r = new Random();
int fileLen = 1;
//把图片做成资源的形式
string[] fileToDisplay ={"DSCF0025.JPG",
"DSCF0029.JPG",
"DSCF0036.JPG",
"DSCF0004.JPG",
"DSCF0012.JPG",
"DSCF0015.JPG",
"DSCF0002.JPG",
"DSCF0010.JPG",
"DSCF0043.JPG",
"DSCF0048.JPG"
};

fileLen = fileToDisplay.Length ;
ResourceManager rm = new ResourceManager ("cCharp.My",Assembly.GetExecutingAssembly ());
Image MyImage = (Bitmap)rm.GetObject(fileToDisplay[r.Next (fileLen)]);
if (MyImage.Width > 800)
{
if (MyImage.Width > MyImage.Height )
{
picBox.Width = 640;
picBox.Height =480;
}
else
{
picBox.Width = 480;
picBox.Height =600;
}
picBox.SizeMode = PictureBoxSizeMode.StretchImage ;
}
else
{
picBox.SizeMode = PictureBoxSizeMode.AutoSize ;
}
picBox.Image = (Image)MyImage;
//
MovePic();
}
...全文
评论 37
收藏 已收藏
分享
返回顶部
yangxingbo
C# 111,129 成员 642,539 内容