首先,建一个windows应用程序,所需控件有pictureBox, OpenFileDialog, textBox, 2个Button
其次,构造函数前添加
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
private string filePath;
添加两个Button的click事件//桌面只能显示bmp图片
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.InitialDirectory = @"C:/"; //打开的初始位置
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; //设置图片显示的大小
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = openFileDialog1.FileName;
string[] strA = this.textBox1.Text.Split('.');
Bitmap bm = new Bitmap(this.textBox1.Text);
//只选择bmp格式的图片
if (strA[1] != "bmp")
{
//filePath = strA[0] + ".bmp";
//bm.Save(filePath);
MessageBox.show("not bmp");
this.button2.enabled = false;
}
else
{
filePath = this.textBox1.Text;
this.pictureBox1.Image = bm;
}
}
}
//设置桌面,其实只用一句话
private void button2_Click(object sender, EventArgs e)
{
int nResult;
if (File.Exists(filePath))
{
nResult = SystemParametersInfo(20, 1, filePath, 0x1 | 0x2);
if (nResult == 0)
{
MessageBox.Show("picture is not changed");
}
else
{
MessageBox.Show("picture has been changed");
}
}
else
{
MessageBox.Show("pic is not existed");
}
}
个人觉得挺不错,所以拿来试试,结果还不错