C# 窗體隨機加載背景圖片

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;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            //隨機加載背景圖片(圖片以數據命名)
            string img_on = "1";
            Random ran = new Random();
            img_on = ran.Next(1, 20).ToString();

            this.BackgroundImage = Image.FromFile(Application.StartupPath + @"\bg_img\" + img_on + ".jpg");  //參數:圖片路徑
            this.BackgroundImageLayout = ImageLayout.Tile;          //圖片平鋪

        }
    }
}

總結:
1.產生隨機數的寫法
2.加載背景圖片
3.相對路徑的引用:Application.StartupPath ,相當於:…\bin\Debug

效果圖:
在這裏插入圖片描述
在這裏插入圖片描述

下載地址:
鏈接:https://pan.baidu.com/s/1GETXdkz0lwhl60UVTK5ZZg
提取碼:s815

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章