frmOne.cs
上传用户:fan366
上传日期:2022-07-21
资源大小:406k
文件大小:23k
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace OnePhoto
- {
- public partial class frmOne : Form
- {
- #region 定义私有变量
-
- /// <summary>
- /// 当前图片名称
- /// </summary>
- private string m_PicName;
- /// <summary>
- /// 当前图片路径
- /// </summary>
- private string m_thePath;
- /// <summary>
- /// 当前图片完全路径
- /// </summary>
- private string m_theFilePath;
- /// <summary>
- /// 当前图片位置
- /// </summary>
- private Point m_PicPoint;
- /// <summary>
- /// 当前光标位置
- /// </summary>
- private Point m_CurPoint;
- /// <summary>
- /// 滚轮事件
- /// </summary>
- private MouseWheeMsg m_MouseWheel;
- #endregion
- /// <summary>
- /// 构造方法
- /// </summary>
- public frmOne()
- {
- InitializeComponent();
- }
- #region 主菜单
- private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- OpenOnePhoto();
- }
- private void 上一张ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- PerPic();
- }
- private void 下一张ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- NextPic();
- }
- private void 收藏ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- SaveAsOneFile();
- }
- private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- CloseOnePhoto();
- }
- private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- }
- private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- }
- private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- }
- private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- }
- private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- }
- private void 放大ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- mimZoomLarg();
- }
- private void 缩小ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- mimZoomSmall();
- }
- private void 适合大小ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- mimDisplay();
- }
- private void 原图ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- mimZoomReal();
- }
- private void 属性ToolStripMenuItem1_Click(object sender, EventArgs e)
- {
- }
- private void 帮助主题ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- }
- private void 技术支持ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- }
- private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- }
- #endregion
- #region 工具栏菜单
- /// <summary>
- /// 打开
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void toolStripButton1_Click(object sender, EventArgs e)
- {
- OpenOnePhoto();
- }
- /// <summary>
- /// 上一张
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void toolStripButton2_Click(object sender, EventArgs e)
- {
- PerPic();
- }
- /// <summary>
- /// 下一张
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void toolStripButton3_Click(object sender, EventArgs e)
- {
- NextPic();
- }
- /// <summary>
- /// 放大
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void toolStripButton4_Click(object sender, EventArgs e)
- {
- mimZoomLarg();
- }
- /// <summary>
- /// 缩小
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void toolStripButton5_Click(object sender, EventArgs e)
- {
- mimZoomSmall();
- }
- /// <summary>
- ///适合大小
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void toolStripButton6_Click(object sender, EventArgs e)
- {
- mimDisplay();
- }
- /// <summary>
- /// 原图
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void toolStripButton7_Click(object sender, EventArgs e)
- {
- mimZoomReal();
- }
- /// <summary>
- /// 收藏
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void toolStripButton8_Click(object sender, EventArgs e)
- {
- SaveAsOneFile();
- }
- /// <summary>
- /// 帮助
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void toolStripButton9_Click(object sender, EventArgs e)
- {
- }
- #endregion
- #region 右键菜单
- private void 收藏ToolStripMenuItem1_Click(object sender, EventArgs e)
- {
- SaveAsOneFile();
- }
- private void 放大ToolStripMenuItem1_Click(object sender, EventArgs e)
- {
- mimZoomLarg();
- }
- private void 缩小ToolStripMenuItem1_Click(object sender, EventArgs e)
- {
- mimZoomSmall();
- }
- private void 适合大小ToolStripMenuItem1_Click(object sender, EventArgs e)
- {
- mimDisplay();
- }
- private void 原图ToolStripMenuItem1_Click(object sender, EventArgs e)
- {
- mimZoomReal();
- }
- private void 属性ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- }
- #endregion
- /// <summary>
- /// 关闭窗体
- /// </summary>
- private void CloseOnePhoto()
- {
- this.Close();
- this.Dispose();
- Application.RemoveMessageFilter(this.m_MouseWheel);
- Application.Exit();
- }
- /// <summary>
- /// 打开图片
- /// </summary>
- private void OpenOnePhoto()
- {
- try
- {
- this.toolStripStatusLabel2.Text = "打开图片文件";
- OpenFileDialog Ofd = new OpenFileDialog();
- Ofd.Filter = "jpg(*.jpg)|*.jpg|jpeg(*.jpeg)|*.jpeg|bmp(*.bmp)|*.bmp|gif(*.gif)|*.gif|tif(*.tif)|*.tif|png(*.png)|*.png";
- Ofd.DefaultExt = "*.jpg";
- //只读
- Ofd.ShowReadOnly = true;
- Ofd.Title = "选择图片文件";
- if (Ofd.ShowDialog() == DialogResult.OK)
- {
- int num = Ofd.FilterIndex;
- string FileType = Ofd.DefaultExt;
- switch(num)
- {
- case 1:
- FileType = "*.jpg";
- break;
- case 2:
- FileType = "*.jpeg";
- break;
- case 3:
- FileType = "*.bmp";
- break;
- case 4:
- FileType = "*.gif";
- break;
- case 5:
- FileType = "*.tif";
- break;
- case 6:
- FileType = "*.png";
- break;
- default:
- FileType = "*.jpg";
- break;
- }
- m_theFilePath = Ofd.FileName;
- ListOnePhoto(m_theFilePath, FileType);
- string theName = Ofd.FileName;
- int FileStart = theName.LastIndexOf("\");
- int FileLen = (theName.Length - FileStart);
- m_PicName = theName.Substring(FileStart + 1, FileLen - 1);
- this.Text = "图片查看器 " + m_PicName;
- this.toolStripStatusLabel1.Text = "名称:" + m_PicName;
- this.toolStripStatusLabel2.Text = "打开图片" + m_PicName;
- this.toolStripStatusLabel3.Text = System.DateTime.Today.ToLongDateString();
- this.pictureBoxPic.Image = Image.FromFile(m_theFilePath);
- this.pictureBoxPic.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
- this.mimZoomReal();
- }
- else
- {
- this.toolStripStatusLabel2.Text = "取消选择图片文件";
- }
- Ofd.Dispose();
- }
- catch (Exception e1)
- {
- this.toolStripStatusLabel2.Text = "打开图片失败,原因:" + e1.Message.ToString() + "!";
- }
- }
- #region 图片放大缩小
- /// <summary>
- /// 放大
- /// </summary>
- private void mimZoomLarg()
- {
- if ((this.pictureBoxPic.Image != null) && (this.pictureBoxPic.Width < 2000))
- {
- this.toolStripStatusLabel2.Text = "放大图片+5%" ;
- this.pictureBoxPic.Width = System.Convert.ToInt32(this.pictureBoxPic.Width * (1 + 0.1));
- this.pictureBoxPic.Height = System.Convert.ToInt32(this.pictureBoxPic.Height * (1 + 0.1));
- int Picx = System.Convert.ToInt32((this.panelImg.Width - this.pictureBoxPic.Width) * 0.5);
- int Picy = System.Convert.ToInt32((this.panelImg.Height - this.pictureBoxPic.Height) * 0.5);
- this.pictureBoxPic.Location = new System.Drawing.Point(Picx, Picy);
- }
- }// end void
- /// <summary>
- /// 缩小
- /// </summary>
- private void mimZoomSmall()
- {
- if (this.pictureBoxPic.Image != null && (this.pictureBoxPic.Width > 100))
- {
- this.toolStripStatusLabel2.Text = "缩小图片-5%";
- this.pictureBoxPic.Width = System.Convert.ToInt32(this.pictureBoxPic.Width / (1 + 0.1));
- this.pictureBoxPic.Height = System.Convert.ToInt32(this.pictureBoxPic.Height / (1 + 0.1));
- int Picx = System.Convert.ToInt32((this.panelImg.Width - this.pictureBoxPic.Width) * 0.5);
- int Picy = System.Convert.ToInt32((this.panelImg.Height - this.pictureBoxPic.Height) * 0.5);
- this.pictureBoxPic.Location = new System.Drawing.Point(Picx, Picy);
- }
- }// end void
- /// <summary>
- /// 原图
- /// </summary>
- private void mimZoomReal()
- {
- if (this.pictureBoxPic.Image != null)
- {
- this.toolStripStatusLabel2.Text = "图片原始大小显示";
- this.pictureBoxPic.Width = this.pictureBoxPic.Image.Width;
- this.pictureBoxPic.Height = this.pictureBoxPic.Image.Height;
- int Picx = System.Convert.ToInt32((this.panelImg.Width - this.pictureBoxPic.Width) * 0.5);
- int Picy = System.Convert.ToInt32((this.panelImg.Height - this.pictureBoxPic.Height) * 0.5);
- this.pictureBoxPic.Location = new System.Drawing.Point(Picx, Picy);
- }
- }// end void
- /// <summary>
- /// 适合大小
- /// </summary>
- private void mimDisplay()
- {
- if (this.pictureBoxPic.Image != null)
- {
- this.toolStripStatusLabel2.Text = "图片适合大小显示";
- int dWidth = this.pictureBoxPic.Image.Width;
- int dHeight = this.pictureBoxPic.Image.Height;
- decimal i = dWidth / dHeight;
- if (i == 0)
- {
- i = 1;
- }
- this.pictureBoxPic.Width = this.panelImg.Width - 16;
- this.pictureBoxPic.Height = System.Convert.ToInt32(this.panelImg.Width / i);
- if (this.pictureBoxPic.Height > this.panelImg.Height)
- {
- this.pictureBoxPic.Height = this.panelImg.Height - 32;
- this.pictureBoxPic.Width = System.Convert.ToInt32(this.panelImg.Height * i);
- }
- int Picx = System.Convert.ToInt32((this.panelImg.Width - this.pictureBoxPic.Width) * 0.5);
- int Picy = System.Convert.ToInt32((this.panelImg.Height - this.pictureBoxPic.Height) * 0.5);
- this.pictureBoxPic.Location = new System.Drawing.Point(Picx, Picy);
- }
- }// end void
- #endregion
- /// <summary>
- /// 加载窗体
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void frmOne_Load(object sender, EventArgs e)
- {
- /////定义鼠标滚轮事件
- this.m_MouseWheel = new MouseWheeMsg();
- Application.AddMessageFilter(this.m_MouseWheel);
- this.m_MouseWheel.MouseWheel += new MouseWheelHandler(MouseWheeMsg_MouseWheel);
- OpenOnePhoto();
- }// end void
- #region 图片移动
- private void pictureBoxPic_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left)
- {
- this.m_PicPoint = new Point(e.X - this.m_CurPoint.X, e.Y - this.m_CurPoint.Y);
- this.m_PicPoint.Offset(this.pictureBoxPic.Location.X, this.pictureBoxPic.Location.Y);
- this.pictureBoxPic.Location = this.m_PicPoint;
- }// end if
- }
- private void pictureBoxPic_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
- {
- this.m_CurPoint = this.pictureBoxPic.PointToClient(System.Windows.Forms.Cursor.Position);
- }
- /// <summary>
- /// 滚轮滚动
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="isDelta"></param>
- private void MouseWheeMsg_MouseWheel(object sender, bool isDelta)
- {
- if (isDelta)
- {
- mimZoomSmall();
- }
- else
- {
- mimZoomLarg();
- }
- }//end void
- private void pictureBoxPic_MouseLeave(object sender, EventArgs e)
- {
- this.Cursor = System.Windows.Forms.Cursors.Default;
- /////////////移出鼠标滚轮事件
- //Application.RemoveMessageFilter( this.m_MouseWheeMsg );
- this.m_MouseWheel.SetHandleType = false;
- }//end void
- private void pictureBoxPic_MouseEnter(object sender, EventArgs e)
- {
- this.Cursor = System.Windows.Forms.Cursors.Hand;
- /////////////增加鼠标滚轮事件
- this.m_MouseWheel.SetHandleType = true;
- //Application.AddMessageFilter( this.m_MouseWheeMsg );
- }//end void
- #endregion
- /// <summary>
- /// 显示图片列表
- /// </summary>
- private void ListOnePhoto(string PicFilePath, string FileType )
- {
-
- int Fileend = PicFilePath.LastIndexOf("\");
- m_thePath = PicFilePath.Substring(0, Fileend);
- try
- {
- this.listView1.Columns.Clear();
- this.listView1.Items.Clear();
- this.listView1.Columns.Add("图片",170);
- if (System.IO.Directory.Exists(m_thePath))
- {
- string[] s = System.IO.Directory.GetFileSystemEntries(m_thePath, FileType);
- int j = s.Length;
- if (j > 0)
- {
- string filesname = null;
- for (int i = 0; i < j; i++)
- {
- string[] thifiles = System.IO.Directory.GetFiles(m_thePath, FileType);
- filesname = thifiles.GetValue(i).ToString().Trim();
- int FileStart = filesname.LastIndexOf("\");
- int FileLen = (filesname.Length - FileStart);
- filesname = filesname.Substring(FileStart + 1, FileLen - 1);
- this.listView1.Items.Add(filesname,0);
- }//end for
- }
- else
- {
- this.toolStripStatusLabel2.Text = "当前路径下没有文件!";
- }
- }// end if
- else
- {
- this.toolStripStatusLabel2.Text = "当前路径无效!请重新选择文件路径!";
- }
- }
- catch (Exception e1)
- {
- this.toolStripStatusLabel2.Text = "选择图片文件异常!";
- string right = e1.Message.ToString();
- }// end catch
- }
- /// <summary>
- /// 获取路径
- /// </summary>
- /// <returns></returns>
- private string GetSavePath()
- {
- string Path = "";
- System.Windows.Forms.FolderBrowserDialog fbdPic = new FolderBrowserDialog();
- if (fbdPic.ShowDialog() == DialogResult.OK)
- {
- Path = fbdPic.SelectedPath;
- }
- return Path;
- }
- /// <summary>
- /// 图片收藏另存
- /// </summary>
- private void SaveAsOneFile()
- {
- try
- {
- System.Drawing.Image theImg = this.pictureBoxPic.Image;
- if (theImg != null )
- {
- string FileName = this.toolStripStatusLabel1.Text.Replace("名称:", "");
- if (FileName.IndexOf(".", 0) <= 0)
- {
- FileName = FileName + ".jpeg";
- }
- string Path = GetSavePath();
- if (Path != "" && Path != null)
- {
- if (!System.IO.Directory.Exists(Path))
- {
- System.IO.Directory.CreateDirectory(Path);
- }
- string FullPath = Path + @"" + FileName;
- this.toolStripStatusLabel2.Text = "正在保存图片 " + FileName;
- this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
- byte[] bytFiles;
- using (System.IO.MemoryStream thems = new System.IO.MemoryStream())
- {
- theImg.Save(thems, System.Drawing.Imaging.ImageFormat.Jpeg);
- bytFiles = thems.ToArray();
- thems.Close();
- }
- using (System.IO.FileStream fs = new System.IO.FileStream(FullPath, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite))
- {
- fs.Write(bytFiles, 0, bytFiles.Length);
- fs.Close();
- }// end using
- this.toolStripStatusLabel2.Text = "图片 " + FileName + " 保存成功!" + "收藏夹路径 " + Path + " !";
- this.Cursor = System.Windows.Forms.Cursors.Default;
- }
- else
- {
- toolStripStatusLabel2.Text = "没有选择一个收藏夹,不能保存图片!";
- }
- }
- else
- {
- toolStripStatusLabel2.Text = "没有选择图片,请打开一个图片!";
- }
- }//try
- catch (Exception e)
- {
- string Sreason = e.Message.ToString();
- toolStripStatusLabel2.Text = "图片保存失败!";
- }// end catch
- }// end void
- private void listView1_SelectedIndexChanged(object sender, EventArgs e)
- {
- //检查是否有选择的项,如果没有选择的项则禁用 打开
- int SelCount = this.listView1.SelectedItems.Count;
- if (SelCount == 1)
- {
- this.m_PicName = this.listView1.SelectedItems[0].SubItems[0].Text;
- this.Text = "图片查看器 " + m_PicName;
- m_theFilePath = this.m_thePath + @"" + m_PicName;
- this.toolStripStatusLabel3.Text = System.DateTime.Today.ToLongDateString();
- this.pictureBoxPic.Image = Image.FromFile(m_theFilePath);
- this.pictureBoxPic.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
- this.mimZoomReal();
- this.toolStripStatusLabel1.Text = "名称:" + m_PicName;
- this.toolStripStatusLabel2.Text = "打开图片" + m_PicName;
- }
- }
- /// <summary>
- /// 下一张图片
- /// </summary>
- /// <returns></returns>
- private void NextPic()
- {
-
- int PicNum = this.listView1.Items.Count;
- for (int i = 0; i < (PicNum - 1); i++)
- {
- string name = this.listView1.Items[i].SubItems[0].Text;
- if (name == this.m_PicName)
- {
- this.m_PicName = this.listView1.Items[i + 1].SubItems[0].Text;
- this.listView1.Items[i + 1].Selected = true;
- break;
- }
- }
-
- }//end void
- /// <summary>
- ///上一张图片
- /// </summary>
- /// <returns></returns>
- private void PerPic()
- {
- int PicNum = this.listView1.Items.Count;
- for (int i = 0; i < (PicNum); i++)
- {
- string name = this.listView1.Items[i].SubItems[0].Text;
- if( (name == this.m_PicName) && ( i > 0 ))
- {
- this.m_PicName = this.listView1.Items[i - 1].SubItems[0].Text;
- this.listView1.Items[ i - 1].Selected = true;
- break;
- }
- }
- }//end void
- }
- }