1. 程式人生 > >C# 實現設定桌面背景圖片的功能

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());
            }
        }
    }
}