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