コード例 #1
0
        /// <summary>
        /// 浏览图片按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string tag = (sender as Button).Tag.ToString().ToLower();

            if (tag == "scan")
            {
                string filePathName = "";                                                            //定义图像文件的位置(包括路径及文件名)
                System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog(); //添加打开对话框
                ofd.Filter = "图像文件|*jpg;*.bmp;*.png,*.tif|所有文件|*.*";                                 //设置过滤器
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)                        //如果确定打开图片,则保存文件的路径及文件名到字符串变量filePathName中
                {
                    filePathName = ofd.FileName;                                                     //包括路径和文件名
                }

                try
                {
                    Bitmap bitmap   = new Bitmap(filePathName);
                    string filepath = "";
                    if (folderExist(System.Environment.CurrentDirectory + "\\Image\\" + projectcode))
                    {
                        filepath = System.Environment.CurrentDirectory + "\\Image\\" + projectcode + "\\" + System.IO.Path.GetFileName(ofd.FileName);
                    }
                    else
                    {
                        folderCreate(System.Environment.CurrentDirectory + "\\Image\\" + projectcode);
                        filepath = System.Environment.CurrentDirectory + "\\Image\\" + projectcode + "\\" + System.IO.Path.GetFileName(ofd.FileName);
                    }

                    bitmap.Save(filepath);
                    bitmap.Dispose();
                    cablePaper.ImagePath = filepath;
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show("工程中已经存在同名的图片,请将图片重命名后再导入", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            else if (tag == "confirm")
            {
                if (CheckPaper())
                {
                    (App.Current.Resources["Locator"] as ViewModelLocator).Main.Papers.Add(cablePaper);
                    SQliteDbContext.AddPaper(cablePaper);
                    string msg = IsModify ? "修改成功!" : "添加成功!";
                    MessageBox.Show(msg, "Info", MessageBoxButton.OK, MessageBoxImage.Information);
                    this.Close();
                }
            }
            else if (tag == "cancel")
            {
                if (!IsModify)
                {
                    foreach (var item in grid.Children)
                    {
                        if (item is TextBox)
                        {
                            (item as TextBox).Text = "";
                        }
                    }
                }
            }
        }