frmMain.cs
上传用户:wedding
上传日期:2022-08-10
资源大小:558k
文件大小:13k
源码类别:

图形图像处理

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. using System.Drawing.Imaging;
  10. namespace MyImageManger
  11. {
  12.     public partial class frmMain : Form
  13.     {
  14.         //变量——用来存放图片的路径
  15.         string filename;
  16.         /// <summary>
  17.         /// 构造函数
  18.         /// </summary>
  19.         public frmMain()
  20.         {
  21.             InitializeComponent();
  22.             //加载皮肤_WindowsXP蓝色风格
  23.             this.skinEngine1.SkinFile = "XPBlue.ssk";
  24.         }
  25.         /// <summary>
  26.         /// 打开图片事件  Ctrl+O 快捷键
  27.         /// </summary>
  28.         /// <param name="sender"></param>
  29.         /// <param name="e"></param>
  30.         private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
  31.         {
  32.             try
  33.             {
  34.                 //打开文件对话框
  35.                 OpenFileDialog file = new OpenFileDialog();
  36.                 //可选的类型
  37.                 file.Filter = "*.*|*.*|*.bmp|*.bmp|GIF files (*.GIF)|*.gif|JPG file(*.JPG)|*.JPG|Icon files(*.ico)|*.ico|Png files(*.png)|*.png";
  38.                 //如果用户点确定,则加载图片
  39.                 if (file.ShowDialog() == DialogResult.OK)
  40.                 {
  41.                     //获取本文件夹下的所有图片
  42.                     ImageManager.GetALLFile(file.FileName);
  43.                     this.OpenFile(file.FileName);
  44.                     
  45.                 }
  46.             }
  47.             catch
  48.             {
  49.                 //如果触发异常捕获,那么肯定是格式不正确
  50.                 MessageBox.Show("您选择的格式此软件无法打开,请确定您所打开的是否为图片","错误提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
  51.             }
  52.         }
  53.         private void frmMain_Load(object sender, EventArgs e)
  54.         {
  55.         }
  56.         /// <summary>
  57.         /// 放大方法
  58.         /// </summary>
  59.         /// <param name="sender"></param>
  60.         /// <param name="e"></param>
  61.         private void tsbtnBig_Click(object sender, EventArgs e)
  62.         {
  63.             this.Bigger();
  64.             ImgLocation();
  65.         }
  66.         /// <summary>
  67.         /// 缩小方法
  68.         /// </summary>
  69.         /// <param name="sender"></param>
  70.         /// <param name="e"></param>
  71.         private void tsBtnSmall_Click(object sender, EventArgs e)
  72.         {
  73.             Smaller();
  74.             ImgLocation();
  75.         }
  76.         /// <summary>
  77.         /// 顺时针90旋转
  78.         /// </summary>
  79.         /// <param name="sender"></param>
  80.         /// <param name="e"></param>
  81.         private void tsbtnRight_Click(object sender, EventArgs e)
  82.         {
  83.             ToRight();
  84.             ImgLocation();
  85.         }
  86.         /// <summary>
  87.         /// 逆时针90度旋转
  88.         /// </summary>
  89.         /// <param name="sender"></param>
  90.         /// <param name="e"></param>
  91.         private void tsBtnLeft_Click(object sender, EventArgs e)
  92.         {
  93.             ToLeft();
  94.             ImgLocation();
  95.         }
  96.         /// <summary>
  97.         /// 删除方法
  98.         /// </summary>
  99.         /// <param name="sender"></param>
  100.         /// <param name="e"></param>
  101.         private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
  102.         {
  103.             //如果用户点击是,则删除
  104.             if (MessageBox.Show("您是否在磁盘删除此图片", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
  105.             {
  106.                 this.pictureBox1.Image = null;
  107.                 //从磁盘删除图片
  108.                 
  109.                 //删除文件
  110.                 ImageManager.imgFileList.Remove(filename);
  111.                 if (ImageManager.imgFileList.Count == 0)
  112.                 {
  113.                     this.pictureBox1.Image = null;
  114.                 }
  115.                 else
  116.                 {
  117.                     if (ImageManager.curImg == ImageManager.imgFileList.Count)
  118.                     {
  119.                         this.OpenFile(ImageManager.imgFileList[0]);
  120.                         ImageManager.curImg = 0;
  121.                     }
  122.                 }
  123.                 File.Delete(filename);
  124.             }
  125.         }
  126.         /// <summary>
  127.         /// 退出系统事件
  128.         /// </summary>
  129.         /// <param name="sender"></param>
  130.         /// <param name="e"></param>
  131.         private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
  132.         {
  133.             if (MessageBox.Show("您确认要退出吗?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
  134.             {
  135.                 this.pictureBox1.Image = null;
  136.                 Application.Exit();
  137.             }
  138.             
  139.         }
  140.         /// <summary>
  141.         /// 另存为事件
  142.         /// </summary>
  143.         /// <param name="sender"></param>
  144.         /// <param name="e"></param>
  145.         private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
  146.         {
  147.             //如果有图片
  148.             if (pictureBox1.Image != null)
  149.             {
  150.                 //截取原文件名
  151.                 string file = filename.Substring(filename.LastIndexOf("\")+1);
  152.                 //打开保存文件框
  153.                 SaveFileDialog _SaveFileDialog = new SaveFileDialog();
  154.                 _SaveFileDialog.Filter = "*.*|*.*|*.bmp|*.bmp|JPG file(*.JPG)|*.JPG|Icon files(*.ico)|*.ico";
  155.                 _SaveFileDialog.FileName = file;
  156.                 //如果用户选择是
  157.                 if (_SaveFileDialog.ShowDialog() == DialogResult.OK)
  158.                 {
  159.                     //往磁盘存放
  160.                     pictureBox1.Image.Save(_SaveFileDialog.FileName, ImageFormat.Jpeg);
  161.                 }
  162.             } 
  163.         }
  164.         /// <summary>
  165.         /// 放大事件
  166.         /// </summary>
  167.         /// <param name="sender"></param>
  168.         /// <param name="e"></param>
  169.         private void 放大ToolStripMenuItem_Click(object sender, EventArgs e)
  170.         {
  171.            
  172.             this.Bigger();
  173.             ImgLocation();
  174.         }
  175.         /// <summary>
  176.         /// 缩小事件
  177.         /// </summary>
  178.         /// <param name="sender"></param>
  179.         /// <param name="e"></param>
  180.         private void 缩小ToolStripMenuItem_Click(object sender, EventArgs e)
  181.         {
  182.             Smaller();
  183.             ImgLocation();
  184.         }
  185.         /// <summary>
  186.         /// 顺时针旋转90度
  187.         /// </summary>
  188.         /// <param name="sender"></param>
  189.         /// <param name="e"></param>
  190.         private void 顺时针旋转ToolStripMenuItem_Click(object sender, EventArgs e)
  191.         {
  192.             this.ToRight();
  193.             ImgLocation();
  194.         }
  195.         /// <summary>
  196.         /// 逆时针旋转90度
  197.         /// </summary>
  198.         /// <param name="sender"></param>
  199.         /// <param name="e"></param>
  200.         private void 逆时针旋转ToolStripMenuItem_Click(object sender, EventArgs e)
  201.         {
  202.             this.ToLeft();
  203.             ImgLocation();
  204.         }
  205.         /// <summary>
  206.         /// 加载图片
  207.         /// </summary>
  208.         /// <param name="filepath"></param>
  209.         private void OpenFile(string filepath)
  210.         {
  211.             try
  212.             {
  213.                 //加载图片
  214.                 //this.pictureBox1.Image = Image.FromFile(filepath);
  215.                 //this.pictureBox1.Load(filepath);
  216.                 //FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
  217.                 //BinaryReader br = new BinaryReader(fs);
  218.                 //byte[] buffer = new byte[1024];
  219.                 //System.Reflection.Assembly thisExe;
  220.                 //thisExe = System.Reflection.Assembly.GetExecutingAssembly();
  221.                 //System.IO.Stream fs = thisExe.GetManifestResourceStream("MyImageManger."+filepath);
  222.                 //this.pictureBox1.Image = Image.FromStream(fs);
  223.                 FileStream fs = File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  224.                 this.pictureBox1.Image = Image.FromStream(fs, false, true);
  225.                 fs.Close();
  226.                 //存储图片名
  227.                 filename = filepath;
  228.                 //操作PictureBox的位置
  229.                 ImgLocation();
  230.             }
  231.             catch
  232.             {
  233.                 //删除文件
  234.                 ImageManager.imgFileList.Remove(filename);
  235.                 if (ImageManager.imgFileList.Count == 0)
  236.                 {
  237.                     this.pictureBox1.Image = null;
  238.                 }
  239.                 else
  240.                 {
  241.                     if (ImageManager.curImg == ImageManager.imgFileList.Count)
  242.                     {
  243.                         this.OpenFile(ImageManager.imgFileList[0]);
  244.                         ImageManager.curImg = 0;
  245.                     }
  246.                 }
  247.             }
  248.                 if (ImageManager.imgFileList.Count > 0)
  249.                 {
  250.                     this.tsBtnNext.Enabled = true;
  251.                     this.tsBtnPre.Enabled = true;
  252.                 }
  253.            
  254.         }
  255.         /// <summary>
  256.         /// 放大方法
  257.         /// </summary>
  258.         private void Bigger()
  259.         {
  260.             if (this.pictureBox1.Image != null)
  261.             {
  262.                 Bitmap bitmap = new Bitmap(this.pictureBox1.Image);
  263.                 this.pictureBox1.Image = ImageManager.KiResizeImage(bitmap, Convert.ToInt32(bitmap.Width * 110 / 100), Convert.ToInt32(bitmap.Height * 110 / 100), 0);
  264.             }
  265.             else
  266.             {
  267.                 MessageBox.Show("请打开一张图片", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  268.             }
  269.         }
  270.         /// <summary>
  271.         /// 缩小方法
  272.         /// </summary>
  273.         private void Smaller()
  274.         {
  275.             if (this.pictureBox1.Image != null)
  276.             {
  277.                 Bitmap bitmap = new Bitmap(this.pictureBox1.Image);
  278.                 this.pictureBox1.Image = ImageManager.KiResizeImage(bitmap, Convert.ToInt32(bitmap.Width * 90 / 100), Convert.ToInt32(bitmap.Height * 90 / 100), 0);
  279.             }
  280.             else
  281.             {
  282.                 MessageBox.Show("请打开一张图片", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  283.             }
  284.         }
  285.         /// <summary>
  286.         /// 顺时针旋转90度方法
  287.         /// </summary>
  288.         private void ToRight()
  289.         {
  290.             //如果有图片
  291.             if (this.pictureBox1.Image != null)
  292.             {
  293.                 Bitmap bitmap = new Bitmap(this.pictureBox1.Image);//获取PictrueBox的图片
  294.                 this.pictureBox1.Image = ImageManager.KiRotate(bitmap, 90, Color.Transparent);//旋转90度
  295.             }
  296.             else//如果无图片
  297.             {
  298.                 MessageBox.Show("请打开一张图片", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  299.             }
  300.         }
  301.         /// <summary>
  302.         /// 逆时针旋转90度方法
  303.         /// </summary>
  304.         private void ToLeft()
  305.         {
  306.             if (this.pictureBox1.Image != null)
  307.             {
  308.                 Bitmap bitmap = new Bitmap(this.pictureBox1.Image);
  309.                 this.pictureBox1.Image = ImageManager.KiRotate(bitmap, -90, Color.Transparent);
  310.             }
  311.             else
  312.             {
  313.                 MessageBox.Show("请打开一张图片", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  314.             }
  315.         }
  316.         /// <summary>
  317.         /// PictureBox位置-保持在中央
  318.         /// </summary>
  319.         private void ImgLocation()
  320.         {
  321.             if (this.pictureBox1.Image != null)
  322.             {
  323.                 Point p = new Point((this.pnlMain.Width - this.pictureBox1.Width) / 2,(this.pnlMain.Height - this.pictureBox1.Height) / 2);
  324.                 this.pictureBox1.Location = p;
  325.                  
  326.             }
  327.         }
  328.         /// <summary>
  329.         /// 窗口大小变化的事件
  330.         /// </summary>
  331.         /// <param name="sender"></param>
  332.         /// <param name="e"></param>
  333.         private void frmMain_Resize(object sender, EventArgs e)
  334.         {
  335.             ImgLocation();
  336.         }
  337.         /// <summary>
  338.         /// 上一张
  339.         /// </summary>
  340.         /// <param name="sender"></param>
  341.         /// <param name="e"></param>
  342.         private void tsBtnPre_Click(object sender, EventArgs e)
  343.         {
  344.             if (ImageManager.curImg == 0)
  345.             {
  346.                 this.OpenFile(ImageManager.imgFileList[ImageManager.imgFileList.Count - 1]);
  347.                 ImageManager.curImg = ImageManager.imgFileList.Count - 1;
  348.             }
  349.             else
  350.             {
  351.                 this.OpenFile(ImageManager.imgFileList[ImageManager.curImg - 1]);
  352.                 ImageManager.curImg -= 1;
  353.             }
  354.         }
  355.         /// <summary>
  356.         /// 下一张
  357.         /// </summary>
  358.         /// <param name="sender"></param>
  359.         /// <param name="e"></param>
  360.         private void tsBtnNext_Click(object sender, EventArgs e)
  361.         {
  362.             if (ImageManager.imgFileList.Count > ImageManager.curImg + 1)
  363.             {
  364.                 this.OpenFile(ImageManager.imgFileList[ImageManager.curImg + 1]);
  365.                 ImageManager.curImg += 1;
  366.             }
  367.             else
  368.             {
  369.                 this.OpenFile(ImageManager.imgFileList[0]);
  370.                 ImageManager.curImg= 0;
  371.             }
  372.         }
  373.     }
  374. }