c#第二次作业 随机显示图片

来源:互联网 发布:中国域名注册商 编辑:程序博客网 时间:2024/06/10 07:24

“随机图片显示器”开发,并记录自程序运行后每张图片已经显示的次数,把次数保存到文本文件中

                                                                       Program.cs

using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Windows.Forms;namespace tupian1{    static class Program    {        /// <summary>        /// 应用程序的主入口点。        /// </summary>        [STAThread]        static void Main()        {            Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);            Application.Run(new Form1());        }    }}

 Form1.cs

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.IO;namespace tupian1{    public partial class Form1 : Form    {        const int COUNT = 5;        int[] nTimes = new int[COUNT];        String strDirImg = @"C:\Users\Administrator\Desktop\新建文件夹";        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            lbl.Text = "显示次数:";            String strPath = strDirImg + "/1.jpg";            pictureBox1.Image = new Bitmap(strPath);            String strPathCount = strDirImg + "count.txt";            StreamReader sd = new StreamReader(strPathCount);            String line;            int nLine = 0;            while ((line =sd.ReadLine())!=null)            {                int n = Convert.ToInt32(line);                nTimes[nLine] = n;                nLine++;            }        }        private void btn_Click(object sender, EventArgs e)        {            Random rnd = new Random();            int num = rnd.Next(1, COUNT + 1);            nTimes[num - 1] = nTimes[num - 1] + 1;            String strPath = strDirImg +"/"+ num + ".jpg";            pictureBox1.Image = new Bitmap(strPath);            lbl.Text = "图片" +  num + ".jpg" + "    显示次数:" + nTimes[num - 1];        }        private void Form1_FormClosed(object sender, FormClosedEventArgs e)        {            String strPathCount = strDirImg + "count.txt";            StreamWriter sw = new StreamWriter(strPathCount);            for (int n = 0; n < COUNT; n++)            {                sw.WriteLine(nTimes[n]);            }            sw.Close();        }    }}

   FormDesigners.cs

namespace tupian1{    partial class Form1    {        /// <summary>        /// 必需的设计器变量。        /// </summary>        private System.ComponentModel.IContainer components = null;        /// <summary>        /// 清理所有正在使用的资源。        /// </summary>        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>        protected override void Dispose(bool disposing)        {            if (disposing && (components != null))            {                components.Dispose();            }            base.Dispose(disposing);        }        #region Windows 窗体设计器生成的代码        /// <summary>        /// 设计器支持所需的方法 - 不要        /// 使用代码编辑器修改此方法的内容。        /// </summary>        private void InitializeComponent()        {            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));            this.pictureBox1 = new System.Windows.Forms.PictureBox();            this.btn = new System.Windows.Forms.Button();            this.lbl = new System.Windows.Forms.Label();            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();            this.SuspendLayout();            //             // pictureBox1            //             this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));            this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));            this.pictureBox1.Location = new System.Drawing.Point(49, 61);            this.pictureBox1.Name = "pictureBox1";            this.pictureBox1.Size = new System.Drawing.Size(177, 107);            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;            this.pictureBox1.TabIndex = 0;            this.pictureBox1.TabStop = false;            //             // btn            //             this.btn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));            this.btn.AutoSize = true;            this.btn.Location = new System.Drawing.Point(149, 218);            this.btn.Name = "btn";            this.btn.Size = new System.Drawing.Size(89, 23);            this.btn.TabIndex = 1;            this.btn.Text = "随机显示";            this.btn.UseVisualStyleBackColor = true;            this.btn.Click += new System.EventHandler(this.btn_Click);            //             // lbl            //             this.lbl.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));            this.lbl.AutoSize = true;            this.lbl.Location = new System.Drawing.Point(49, 190);            this.lbl.Name = "lbl";            this.lbl.Size = new System.Drawing.Size(41, 12);            this.lbl.TabIndex = 2;            this.lbl.Text = "label1";            //             // Form1            //             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;            this.ClientSize = new System.Drawing.Size(284, 262);            this.Controls.Add(this.lbl);            this.Controls.Add(this.btn);            this.Controls.Add(this.pictureBox1);            this.Name = "Form1";            this.Text = "Form1";            this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);            this.Load += new System.EventHandler(this.Form1_Load);            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();            this.ResumeLayout(false);            this.PerformLayout();        }        #endregion        private System.Windows.Forms.PictureBox pictureBox1;        private System.Windows.Forms.Button btn;        private System.Windows.Forms.Label lbl;    }}

运行结果:



0 0