C#.Net小程序运行显示:内存不足问题???

_明月 2016-04-12 10:51:12

先介绍下我的这个小程序。该程序目的是:PictureBox控件在由Timer控件制定的时间下,自动随机变换指定路径文件夹下的图片,在图片自动变换的同时播放一首.WAV音乐。

主界面:6个PictureBox控件 + 1个Timer控件。 6个PictureBox控件是为了获取图片,初始时将同一张图片赋给各个PictureBox控件,Timer控件的主要作用就是为了设置一个自动变换的时间。下面是该程序的具体代码。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;

namespace Test_160322_004
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
//播放音乐
SoundPlayer sp = new SoundPlayer();
sp.SoundLocation = @"C:\Users\Administrator\Desktop\8.wav";
sp.Play();

//加载图片
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox3.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox4.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox5.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox6.SizeMode = PictureBoxSizeMode.Zoom;


//在窗体加载时,给每一个PictureBox都赋值一张图片的路径
pictureBox1.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\Picture\001.jpg");
pictureBox2.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\Picture\001.jpg");
pictureBox3.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\Picture\001.jpg");
pictureBox4.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\Picture\00.jpg");
pictureBox5.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\Picture\001.jpg");
pictureBox6.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\Picture\001.jpg");


}

string[] path = System.IO.Directory.GetFiles(@"C:\Users\Administrator\Desktop\Picture");
//int i = 0;
Random r = new Random(); //使用随机数组r将整型变量i替换掉。

private void timer1_Tick(object sender, EventArgs e)
{

pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]);
pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]);
pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]);
pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]);
pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]);
pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]); //开始运行,即报错:内存不足???

}
}
}



这个程序在运行时,出现了如图片1所示的问题。我不知道这个问题为什么会出现,这个问题又该如何解决呢?请指教,谢谢。


图片1



在最后分享一首我喜欢的诗


金缕衣

杜秋娘 [唐]

劝君莫惜金缕衣,劝君惜取少年时。

花开堪折直须折,莫待无花空折枝。




我美丽的校园




校园的花开了,真的非常漂亮。待得金秋的九月到临时,校园满院的桂花将会悄然绽放,那时学校将沉浸在沁人心脾的桂香的海洋中。

“有花堪折直须折,莫待无花空折枝。”是一句千古名句,值得深味

...全文
723 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
_明月 2016-04-15
  • 打赏
  • 举报
回复
经过我自己制作了一个类似的小程序后,发现在Picture文件夹中,其中有一张图片所占内存大小为:1.58MB。正是因为该图片所占内存较大,所以才出现了“内存不足”的问题。将该图片从Picture文件中取消掉后,程序运行就不在报错了。 @qq451354 @bbwolfcool @楚狂歌 @xian_wwq 谢谢以上各位。
_明月 2016-04-13
  • 打赏
  • 举报
回复
引用 5 楼 xian_wwq 的回复:
timer 的interval不会是毫秒吧 有一种可能,单个周期内,加载没有完成, 导致timer重入了
这个Timer的Interval的单位确实是毫秒,不过是1000毫秒,也就是1秒。我下午做一个实验看一看修改Timer控件的Interval属性能否解决这个问题,谢谢。
_明月 2016-04-13
  • 打赏
  • 举报
回复
引用 3 楼 u011266608 的回复:
图片数量比较少的话,先全部读到Image数组中,然后再随机好了----仅仅解决你目前的问题
这个图片文件夹中放了有10张图片。另外,这个窗体界面有6个PictureBox控件。这个按理,图片应该不多的! 下午有时间我自己做一个只有一个PictureBox控件、一个Timer控件的小窗体试一试看结果会怎样。谢谢你的回复。
xian_wwq 2016-04-13
  • 打赏
  • 举报
回复
[quote=引用 楼主 dear_Alice_moon 的回复:] 先介绍下我的这个小程序。该程序目的是:PictureBox控件在由Timer控件制定的时间下,自动随机变换指定路径文件夹下的图片,在图片自动变换的同时播放一首.WAV音乐。 主界面:6个PictureBox控件 + 1个Timer控件。 6个PictureBox控件是为了获取图片,初始时将同一张图片赋给各个PictureBox控件,Timer控件的主要作用就是为了设置一个自动变换的时间。下面是该程序的具体代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;

namespace Test_160322_004
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //播放音乐
            SoundPlayer sp = new SoundPlayer();
            sp.SoundLocation = @"C:\Users\Administrator\Desktop\8.wav";
            sp.Play();

            //加载图片
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox3.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox4.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox5.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox6.SizeMode = PictureBoxSizeMode.Zoom;


            //在窗体加载时,给每一个PictureBox都赋值一张图片的路径
            pictureBox1.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\Picture\001.jpg");
            pictureBox2.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\Picture\001.jpg");
            pictureBox3.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\Picture\001.jpg");
            pictureBox4.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\Picture\00.jpg");
            pictureBox5.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\Picture\001.jpg");
            pictureBox6.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\Picture\001.jpg");


        }

        string[] path = System.IO.Directory.GetFiles(@"C:\Users\Administrator\Desktop\Picture");
        //int i = 0;
        Random r = new Random();   //使用随机数组r将整型变量i替换掉。

        private void timer1_Tick(object sender, EventArgs e)
        {

            pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]);
            pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]);
            pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]);
            pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]);
            pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]);
            pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]);    //开始运行,即报错:内存不足???

        }
    }
}

这个程序在运行时,出现了如图片1所示的问题。我不知道这个问题为什么会出现,这个问题又该如何解决呢?请指教,谢谢。
图片1
在最后分享一首我喜欢的诗
金缕衣
杜秋娘 [唐]
劝君莫惜金缕衣,劝君惜取少年时。
花开堪折直须折,莫待无花空折枝。
我美丽的校园
校园的花开了,真的非常漂亮。待得金秋的九月到临时,校园满院的桂花将会悄然绽放,那时学校将沉浸在沁人心脾的桂香的海洋中。 “有花堪折直须折,莫待无花空折枝。”是一句千古名句,值得深味 /quote] timer 的interval不会是毫秒吧 有一种可能,单个周期内,加载没有完成, 导致timer重入了
_明月 2016-04-13
  • 打赏
  • 举报
回复
引用 8 楼 qq451354 的回复:
不要用这种方式读照片 using (FileStream file = new FileStream(ImagInfo.FileName, FileMode.Open)) { byte[] b = new byte[file.Length]; file.Read(b, 0, b.Length); MemoryStream stream = new MemoryStream(b); Image image = Image.FromStream(stream); imageSlider.Images.Add(image); file.Close(); }
嗯,这种读取照片的方式是我自己在学习”传智播客“时的一个学习视频时,视频中老师讲授的。我现在学习的视频设计C#.Net方面的知识比较浅,等以后知识学深了,应该就不会使用这种方式读取文件了。 这种方式适合读取小文件,读取大文件要涉及到断点续传。“断点续传”应该会在之后的学习视频中学习到的。谢谢你的回复,谢谢。
ChengYz_ 2016-04-13
  • 打赏
  • 举报
回复
不要用这种方式读照片 using (FileStream file = new FileStream(ImagInfo.FileName, FileMode.Open)) { byte[] b = new byte[file.Length]; file.Read(b, 0, b.Length); MemoryStream stream = new MemoryStream(b); Image image = Image.FromStream(stream); imageSlider.Images.Add(image); file.Close(); }
_明月 2016-04-12
  • 打赏
  • 举报
回复
引用 1楼bbwolfcool 的回复:
小同志,你为什么 pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]); pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]); pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]); pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]); pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]); pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]); //开始运行 那么多Box1,一个控件搞那么多图片有什么效果?
这个是我按照自己学习的视频上老师做的,我就自己做了一个。 我觉得视频中老师这么做一是为了让学生熟悉PictureBox控件;二是设计的这个窗体运行显示图片是给人一种视觉上的美观;三是为了顺便复习一下Random(随机数)吧。这个是我个人的猜测。
_明月 2016-04-12
  • 打赏
  • 举报
回复
我现在正在用手机版客户端回复本则帖子,不知道回复的内容在电脑上显示的又怎样。 我这里有下雨了,下了近6天的雨了!整体下雨,真的特烦人!弄的宿舍都感到有些潮湿!蜘蛛都在电杠与吊扇上结网了,我床铺周围刚刚飞了一只好大的昆虫。对此,我表示真的很无奈! 不聊了,晚安。
楚狂歌 2016-04-12
  • 打赏
  • 举报
回复
图片数量比较少的话,先全部读到Image数组中,然后再随机好了----仅仅解决你目前的问题
bbwolfcool 2016-04-12
  • 打赏
  • 举报
回复
小同志,你为什么 pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]); pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]); pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]); pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]); pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]); pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]); //开始运行 那么多Box1,一个控件搞那么多图片有什么效果?

110,566

社区成员

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

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

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