frmMain.cs
上传用户:wedding
上传日期:2022-08-10
资源大小:558k
文件大小:13k
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Drawing.Imaging;
- namespace MyImageManger
- {
- public partial class frmMain : Form
- {
- //变量——用来存放图片的路径
- string filename;
- /// <summary>
- /// 构造函数
- /// </summary>
- public frmMain()
- {
- InitializeComponent();
- //加载皮肤_WindowsXP蓝色风格
- this.skinEngine1.SkinFile = "XPBlue.ssk";
- }
- /// <summary>
- /// 打开图片事件 Ctrl+O 快捷键
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- try
- {
- //打开文件对话框
- OpenFileDialog file = new OpenFileDialog();
- //可选的类型
- file.Filter = "*.*|*.*|*.bmp|*.bmp|GIF files (*.GIF)|*.gif|JPG file(*.JPG)|*.JPG|Icon files(*.ico)|*.ico|Png files(*.png)|*.png";
- //如果用户点确定,则加载图片
- if (file.ShowDialog() == DialogResult.OK)
- {
- //获取本文件夹下的所有图片
- ImageManager.GetALLFile(file.FileName);
- this.OpenFile(file.FileName);
-
- }
- }
- catch
- {
- //如果触发异常捕获,那么肯定是格式不正确
- MessageBox.Show("您选择的格式此软件无法打开,请确定您所打开的是否为图片","错误提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
- }
- }
- private void frmMain_Load(object sender, EventArgs e)
- {
- }
- /// <summary>
- /// 放大方法
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void tsbtnBig_Click(object sender, EventArgs e)
- {
- this.Bigger();
- ImgLocation();
- }
- /// <summary>
- /// 缩小方法
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void tsBtnSmall_Click(object sender, EventArgs e)
- {
- Smaller();
- ImgLocation();
- }
- /// <summary>
- /// 顺时针90旋转
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void tsbtnRight_Click(object sender, EventArgs e)
- {
- ToRight();
- ImgLocation();
- }
- /// <summary>
- /// 逆时针90度旋转
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void tsBtnLeft_Click(object sender, EventArgs e)
- {
- ToLeft();
- ImgLocation();
- }
- /// <summary>
- /// 删除方法
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- //如果用户点击是,则删除
- if (MessageBox.Show("您是否在磁盘删除此图片", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
- {
- this.pictureBox1.Image = null;
- //从磁盘删除图片
-
- //删除文件
- ImageManager.imgFileList.Remove(filename);
- if (ImageManager.imgFileList.Count == 0)
- {
- this.pictureBox1.Image = null;
- }
- else
- {
- if (ImageManager.curImg == ImageManager.imgFileList.Count)
- {
- this.OpenFile(ImageManager.imgFileList[0]);
- ImageManager.curImg = 0;
- }
- }
- File.Delete(filename);
- }
- }
- /// <summary>
- /// 退出系统事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (MessageBox.Show("您确认要退出吗?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
- {
- this.pictureBox1.Image = null;
- Application.Exit();
- }
-
- }
- /// <summary>
- /// 另存为事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- //如果有图片
- if (pictureBox1.Image != null)
- {
- //截取原文件名
- string file = filename.Substring(filename.LastIndexOf("\")+1);
- //打开保存文件框
- SaveFileDialog _SaveFileDialog = new SaveFileDialog();
- _SaveFileDialog.Filter = "*.*|*.*|*.bmp|*.bmp|JPG file(*.JPG)|*.JPG|Icon files(*.ico)|*.ico";
- _SaveFileDialog.FileName = file;
- //如果用户选择是
- if (_SaveFileDialog.ShowDialog() == DialogResult.OK)
- {
- //往磁盘存放
- pictureBox1.Image.Save(_SaveFileDialog.FileName, ImageFormat.Jpeg);
- }
- }
- }
- /// <summary>
- /// 放大事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void 放大ToolStripMenuItem_Click(object sender, EventArgs e)
- {
-
- this.Bigger();
- ImgLocation();
- }
- /// <summary>
- /// 缩小事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void 缩小ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Smaller();
- ImgLocation();
- }
- /// <summary>
- /// 顺时针旋转90度
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void 顺时针旋转ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- this.ToRight();
- ImgLocation();
- }
- /// <summary>
- /// 逆时针旋转90度
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void 逆时针旋转ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- this.ToLeft();
- ImgLocation();
- }
- /// <summary>
- /// 加载图片
- /// </summary>
- /// <param name="filepath"></param>
- private void OpenFile(string filepath)
- {
- try
- {
- //加载图片
- //this.pictureBox1.Image = Image.FromFile(filepath);
- //this.pictureBox1.Load(filepath);
- //FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
- //BinaryReader br = new BinaryReader(fs);
- //byte[] buffer = new byte[1024];
- //System.Reflection.Assembly thisExe;
- //thisExe = System.Reflection.Assembly.GetExecutingAssembly();
- //System.IO.Stream fs = thisExe.GetManifestResourceStream("MyImageManger."+filepath);
- //this.pictureBox1.Image = Image.FromStream(fs);
- FileStream fs = File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
- this.pictureBox1.Image = Image.FromStream(fs, false, true);
- fs.Close();
- //存储图片名
- filename = filepath;
- //操作PictureBox的位置
- ImgLocation();
- }
- catch
- {
- //删除文件
- ImageManager.imgFileList.Remove(filename);
- if (ImageManager.imgFileList.Count == 0)
- {
- this.pictureBox1.Image = null;
- }
- else
- {
- if (ImageManager.curImg == ImageManager.imgFileList.Count)
- {
- this.OpenFile(ImageManager.imgFileList[0]);
- ImageManager.curImg = 0;
- }
- }
- }
- if (ImageManager.imgFileList.Count > 0)
- {
- this.tsBtnNext.Enabled = true;
- this.tsBtnPre.Enabled = true;
- }
-
- }
- /// <summary>
- /// 放大方法
- /// </summary>
- private void Bigger()
- {
- if (this.pictureBox1.Image != null)
- {
- Bitmap bitmap = new Bitmap(this.pictureBox1.Image);
- this.pictureBox1.Image = ImageManager.KiResizeImage(bitmap, Convert.ToInt32(bitmap.Width * 110 / 100), Convert.ToInt32(bitmap.Height * 110 / 100), 0);
- }
- else
- {
- MessageBox.Show("请打开一张图片", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- }
- /// <summary>
- /// 缩小方法
- /// </summary>
- private void Smaller()
- {
- if (this.pictureBox1.Image != null)
- {
- Bitmap bitmap = new Bitmap(this.pictureBox1.Image);
- this.pictureBox1.Image = ImageManager.KiResizeImage(bitmap, Convert.ToInt32(bitmap.Width * 90 / 100), Convert.ToInt32(bitmap.Height * 90 / 100), 0);
- }
- else
- {
- MessageBox.Show("请打开一张图片", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- }
- /// <summary>
- /// 顺时针旋转90度方法
- /// </summary>
- private void ToRight()
- {
- //如果有图片
- if (this.pictureBox1.Image != null)
- {
- Bitmap bitmap = new Bitmap(this.pictureBox1.Image);//获取PictrueBox的图片
- this.pictureBox1.Image = ImageManager.KiRotate(bitmap, 90, Color.Transparent);//旋转90度
- }
- else//如果无图片
- {
- MessageBox.Show("请打开一张图片", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- }
- /// <summary>
- /// 逆时针旋转90度方法
- /// </summary>
- private void ToLeft()
- {
- if (this.pictureBox1.Image != null)
- {
- Bitmap bitmap = new Bitmap(this.pictureBox1.Image);
- this.pictureBox1.Image = ImageManager.KiRotate(bitmap, -90, Color.Transparent);
- }
- else
- {
- MessageBox.Show("请打开一张图片", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- }
- /// <summary>
- /// PictureBox位置-保持在中央
- /// </summary>
- private void ImgLocation()
- {
- if (this.pictureBox1.Image != null)
- {
- Point p = new Point((this.pnlMain.Width - this.pictureBox1.Width) / 2,(this.pnlMain.Height - this.pictureBox1.Height) / 2);
- this.pictureBox1.Location = p;
-
- }
- }
- /// <summary>
- /// 窗口大小变化的事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void frmMain_Resize(object sender, EventArgs e)
- {
- ImgLocation();
- }
- /// <summary>
- /// 上一张
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void tsBtnPre_Click(object sender, EventArgs e)
- {
- if (ImageManager.curImg == 0)
- {
- this.OpenFile(ImageManager.imgFileList[ImageManager.imgFileList.Count - 1]);
- ImageManager.curImg = ImageManager.imgFileList.Count - 1;
- }
- else
- {
- this.OpenFile(ImageManager.imgFileList[ImageManager.curImg - 1]);
- ImageManager.curImg -= 1;
- }
- }
- /// <summary>
- /// 下一张
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void tsBtnNext_Click(object sender, EventArgs e)
- {
- if (ImageManager.imgFileList.Count > ImageManager.curImg + 1)
- {
- this.OpenFile(ImageManager.imgFileList[ImageManager.curImg + 1]);
- ImageManager.curImg += 1;
- }
- else
- {
- this.OpenFile(ImageManager.imgFileList[0]);
- ImageManager.curImg= 0;
- }
- }
- }
- }