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();
}
///
/// 引用user32.dll的包。
///
///
///
///
///
///
[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;
///
/// 是把获取的图片信息放入到ListView中
///
///
///
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";
}
///
/// 设置为桌面背景图片的功能。
///
///
///
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的话在重启后就变回原来的了
}
}
}
///
/// 实现图片预览的功能。。
///
///
///
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());
}
}
}
}