C# 调用画图工具打开图片

来源:互联网 发布:网络教学和函授 编辑:程序博客网 时间:2024/05/18 01:22
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Diagnostics;

namespace WinFormBrowserImage
{
    public partial class FormImage : Form
    {
        #region
        private DataTable table;
        private PictureBox picture;
        private Splitter split;
        private OpenFileDialog openFile;
        #endregion

        public FormImage()
        {
            #region
            InitializeComponent();
            openFile = new OpenFileDialog();
            openFile.Filter = "图像格式(*.BMP;*.GIF;*.JPG;*.PNG)|*.bmp;*.gif;*.jpg;*.png";
            this.AllowDrop = true;
            this.HelpButton = true;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.BackgroundImageLayout = ImageLayout.Zoom;
            this.StartPosition = FormStartPosition.WindowsDefaultBounds;
            this.Text = "单击标题栏里的帮助按钮可加载图片";
            #endregion
        }

        protected override void OnDragEnter(DragEventArgs e)
        {
            base.OnDragEnter(e);
            this.Activate();
            DataObject data = e.Data as DataObject;
            if (data.ContainsFileDropList())
            {
                FileInfo info = new FileInfo(data.GetFileDropList()[0]);
                if (Regex.IsMatch(info.Extension, @".(bmp|gif|jpg|png)", RegexOptions.IgnoreCase)) // 指定不区分大小写的匹配。
                {
                    this.Text = info.Name;
                    this.BackgroundImage = Image.FromFile(info.FullName);
                    Environment.CurrentDirectory = info.DirectoryName;
                    openFile.FileName = info.FullName;
                }
            }
        }

        protected override void OnHelpButtonClicked(CancelEventArgs e)
        {
            base.OnHelpButtonClicked(e);
            e.Cancel = true;
            if (openFile.ShowDialog(this) == DialogResult.OK)
            {
                this.Text = openFile.SafeFileName;
                this.BackgroundImage = Image.FromFile(openFile.FileName);
            }
        }

        protected override void OnDoubleClick(EventArgs e)
        {
            base.OnDoubleClick(e);
            if (File.Exists(this.Text))
            {
                Process.Start("mspaint.exe", this.Text);
                //using (Process psi = new Process())
                //{
                //    ProcessStartInfo info = psi.StartInfo;
                //    info.FileName = "mspaint.exe"; // 画图工具。
                //    info.Arguments = this.Text;
                //    info.WorkingDirectory = Environment.CurrentDirectory; // 当前工作目录。
                //    info.WindowStyle = ProcessWindowStyle.Maximized; // 窗口最大化。
                //    psi.Start();
                //}
            }
        }
    }
}

原创粉丝点击