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;
using System.Runtime.InteropServices;
using System.IO;
using System.Drawing.Imaging;

namespace zhuomianimg
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 引用user32.dll的包。
        /// </summary>
        /// <param name="uAction"></param>
        /// <param name="uParam"></param>
        /// <param name="lpvparam"></param>
        /// <param name="fuwinIni"></param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "SystemParametersInfoA")]
        static extern Int32 SystemParametersInfo(Int32 uAction, Int32 uParam, string lpvparam, Int32 fuwinIni);
        private const int SPI_SETDESKWALLPAPER = 20;
        Form2 frm2;
        /// <summary>
        /// 是把获取的图片信息放入到ListView中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
               
                listView1.Items.Clear();
                string[] files = openFileDialog1.FileNames;
                string[] fileinfo = new string[3];
                for (int i = 0; i < files.Length; i++)
                {
                    string path = files[i].ToString();
                    string fileName = path.Substring(path.LastIndexOf("//") + 1, path.Length - 1 - path.LastIndexOf("//"));
                    string filetype = fileName.Substring(fileName.LastIndexOf(".") + 1, fileName.Length - 1 - fileName.LastIndexOf("."));
                    listView1.GridLines = true;
                    listView1.Sorting = SortOrder.Ascending;
                    fileinfo[0] = fileName;
                    fileinfo[1] = path;
                    fileinfo[2] = filetype;
                    ListViewItem lvi = new ListViewItem(fileinfo,0);
                
                    listView1.Items.Add(lvi);
                }
            
            }
        }

     

        private void Form1_Load(object sender, EventArgs e)
        {
            //int x = this.Location.X;
            //int y = this.Location.Y;
            //frm2 = new Form2();
            //Point showpoint = new Point(Convert.ToInt32(x + this.Width), Convert.ToInt32(y));
            //frm2.Location = this.PointToScreen(showpoint);
            //frm2.Hide();
            openFileDialog1.Filter = "支持图片格式|*.jpeg;*.png;*.bmp;*.jpg";

        }

        /// <summary>
        /// 设置为桌面背景图片的功能。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                string fpath = listView1.SelectedItems[0].SubItems[1].Text;
                string sfiletype = fpath.Substring(fpath.LastIndexOf(".")+1,(fpath.Length-fpath.LastIndexOf(".")-1));
                sfiletype = sfiletype.ToLower();
                string sfilename = fpath.Substring(fpath.LastIndexOf("//")+1,(fpath.LastIndexOf(".")-fpath.LastIndexOf("//")-1));
               
                if (sfiletype == "bmp")        //判断文件类型是否是bmp格式。。
                {
                    SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, fpath, 1); //调用,filename为图片地址,最后一个参数需要为1,0的话在重启后就变回原来的了

                }
                else
                {
                    string SystemPath = Environment.SystemDirectory;     //获取系统路径;
                    string path = SystemPath + "//" + sfilename + ".bmp";
                    FileInfo fi = new FileInfo(path);
                    if (fi.Exists)                                      //判断文件是否存在?
                    {
                        fi.Delete();
                        PictureBox pb = new PictureBox();
                        pb.Image = Image.FromFile(fpath);
                        pb.Image.Save(SystemPath + "//" + sfilename + ".bmp", ImageFormat.Bmp);
                    }
                    else
                    {
                        PictureBox pb = new PictureBox();
                        pb.Image = Image.FromFile(fpath);
                        pb.Image.Save(SystemPath+"//"+sfilename+".bmp",ImageFormat.Bmp);
                    }
                    SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, path, 1);  //调用,filename为图片地址,最后一个参数需要为1,0的话在重启后就变回原来的了
                }
            }
         }


        /// <summary>
        /// 实现图片预览的功能。。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (listView1.SelectedItems.Count > 0)
            {

                string fpath = listView1.SelectedItems[0].SubItems[1].Text;
                string sfiletype = fpath.Substring(fpath.LastIndexOf(".") + 1, (fpath.Length - fpath.LastIndexOf(".") - 1));
                sfiletype = sfiletype.ToLower();
                string sfilename = fpath.Substring(fpath.LastIndexOf("//") + 1, (fpath.LastIndexOf(".") - fpath.LastIndexOf("//") - 1));

 

                string SystemPath = Environment.SystemDirectory;     //获取系统路径;
                string path = SystemPath + "//" + sfilename + ".bmp";
                FileInfo fi = new FileInfo(path);
                if (fi.Exists)                                      //判断文件是否存在?
                {
                    fi.Delete();
                    pictureBox1.Image = Image.FromFile(fpath);
                    pictureBox1.Image.Save(SystemPath + "//" + sfilename + ".bmp", ImageFormat.Bmp);
                }
                else
                {

                    pictureBox1.Image = Image.FromFile(fpath);
                    pictureBox1.Image.Save(SystemPath + "//" + sfilename + ".bmp", ImageFormat.Bmp);
                }


                MessageBox.Show("图片的宽:"+this.pictureBox1.Width.ToString());
               MessageBox.Show( "图片的高:"+this.pictureBox1.Height.ToString());
            }
        }
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值