zoukankan      html  css  js  c++  java
  • PictureBox的内存问题

    1、PictureBox类

      表示用于显示图像的 Windows 图片框控件,通常使用 PictureBox 来显示位图、元文件、图标、JPEG、GIF 或 PNG 文件中的图形。

                                                              —MSDN

    2、PictureBox控件的使用

      工具箱中的公共控件中托出PictureBox控件

      设置SizeMode属性—枚举类型

     PictureBox.Image获得图片的路径:三种方法

      1.绝对路径: 
      this.pictureBox2.Image=Image.FromFile("D:\001.jpg"); 

      2.相对路径: 
      Application.StartupPath;  
      可以得到程序根目录  
      this.pictureBox2.Image=Image.FromFile(Application.StartupPath "\1.gif"); 

      3.获得网络图片的路径 
      this.pictureBox2.Image= Image.FromStream(System.Net.WebRequest.Create(http://www.pxkt.com/logo.gif).GetResponse().GetResponseStream());
     
    注:1、获得指定文件夹的所有文件的全路径

    string [] path=Directory.GetFiles(@"......");

         2、如果不加 if (pictureBox1.Image != null)pictureBox1.Image.Dispose();会导致内存的溢出而报错。

    示例:

       /// <summary>
            /// 程序进入的时候加载函数,给每个picturebox给一张照片
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void Form1_Load(object sender, EventArgs e)
            {
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.Image = Image.FromFile(path[i]);
                pictureBox2.Image = Image.FromFile(@"C:UsersAdministratorDesktop4\_DSC2211.jpg");
                pictureBox3.Image = Image.FromFile(@"C:UsersAdministratorDesktop4\_DSC2211.jpg");
            }
            string[] path = Directory.GetFiles(@"C:UsersAdministratorDesktop4");
            int i = 0;
            Random r = new Random();
            /// <summary>
            /// 定时器控制每隔一秒换一张图片
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void timer1_Tick(object sender, EventArgs e)
            {
                i++;
                if (i == path.Length)
                {
                    i = 0;
                }
             if (pictureBox1.Image != null)pictureBox1.Image.Dispose();
             if (pictureBox2.Image != null) pictureBox2.Image.Dispose();
             if (pictureBox3.Image != null) pictureBox3.Image.Dispose();
             pictureBox1.Image = Image.FromFile(path[r.Next(0,path.Length)]);
             pictureBox2.Image = Image.FromFile(path[r.Next(0,path.Length)]);
             pictureBox3.Image = Image.FromFile(path[r.Next(0,path.Length)]);
            }   

     值得注意的是如果不加

     if (pictureBox1.Image != null)pictureBox1.Image.Dispose();
    会导致内存的溢出。
  • 相关阅读:
    svn不提交.net项目中的bin
    java 与C# 时间格式 交互
    生成菜单序列号
    ashx 方法模板
    webservice使用方法
    如何配置IIS服务器?
    zepto.fullpage
    模拟单选框
    star score
    Hover show tips
  • 原文地址:https://www.cnblogs.com/apollo-shen/p/6876126.html
Copyright © 2011-2022 走看看