/// <summary> /// 添加广告 /// </summary> private void AddAdvertExecute() { AdvertWindow window = new AdvertWindow() { DataContext = this, Creater = _accName, Owner = Application.Current.MainWindow, }; string fullFilename = string.Empty; string fileName = string.Empty; window.OpenImage += () => { OpenFileDialog dlg = new OpenFileDialog(); //定义一个打开文件对话框 dlg.Multiselect = false; //指示用户不可以选择多个文件 dlg.Filter = "JPG 图片 (*.jpg)|*.jpg|PNG 图片 (*.png)|*.png"; //指定用户只能上传jpg和png格式的图片 bool? result = dlg.ShowDialog(); //打开对话框 if (result != null && result == true) //如果单击了确定按钮并且选择了文件 { Stream inputStream = dlg.OpenFile(); BitmapImage image = new BitmapImage(); //image就是要预览的图片 fullFilename = dlg.FileName; //获得全路径文件名 fileName = dlg.SafeFileName;//文件名 //string extension = name.Substring(name.LastIndexOf('.'), name.Length - name.LastIndexOf('.')); //取得扩展名(包括“.”) //以流的形式初始化图片 image.BeginInit(); image.StreamSource = inputStream; image.EndInit(); window.ShowImage(image); } }; if (window.ShowDialog() == true) { try { string sUrl = ConnectConfigData.UpLoadImage; HttpUploadFile(sUrl, fullFilename); } catch (Exception ex) { MessageBox.Show("广告图片上传失败!", "提示信息"); return; } AdvertInfo info = new AdvertInfo(); info.ID = Guid.NewGuid().ToString().Replace("-", ""); info.Name = window.AdvertName; info.CreateDate = DateTime.Now; info.Creator = _accName; info.Remark = window.Remark; info.Status = window.State; info.Url = fileName; ErrType err = _businessService.AddAdvert(_loginID, info); if (err.Err == ERR.SUCCESS) { MessageBox.Show("添加成功!", "提示信息", MessageBoxButton.OK, MessageBoxImage.Information); return; } else { MessageBox.Show(err.ErrMsg, err.ErrTitle, MessageBoxButton.OK, MessageBoxImage.Error); } } }
/// <summary> /// 编辑广告 /// </summary> private void EditAdvertExecute() { string downLoadUrl = "http://121.40.153.156:8075/AdviceImage/" + CurAdvert.Url; BitmapImage dImg = DownImage(downLoadUrl); AdvertWindow window = new AdvertWindow() { DataContext = this, Owner = Application.Current.MainWindow, AdvertName = CurAdvert.Name, Creater = CurAdvert.Creator, State = CurAdvert.Status, Remark = CurAdvert.Remark }; window.ShowImage(dImg); string fullFilename = string.Empty; string fileName = string.Empty; bool isOpened = false; window.OpenImage += () => { OpenFileDialog dlg = new OpenFileDialog(); //定义一个打开文件对话框 dlg.Multiselect = false; //指示用户不可以选择多个文件 dlg.Filter = "JPG 图片 (*.jpg)|*.jpg|PNG 图片 (*.png)|*.png"; //指定用户只能上传jpg和png格式的图片 bool? result = dlg.ShowDialog(); //打开对话框 if (result != null && result == true) //如果单击了确定按钮并且选择了文件 { Stream inputStream = dlg.OpenFile(); BitmapImage image = new BitmapImage(); //image就是要预览的图片 fullFilename = dlg.FileName; //获得全路径文件名 fileName = dlg.SafeFileName;//文件名 //string extension = name.Substring(name.LastIndexOf('.'), name.Length - name.LastIndexOf('.')); //取得扩展名(包括“.”) //以流的形式初始化图片 image.BeginInit(); image.StreamSource = inputStream; image.EndInit(); window.ShowImage(image); isOpened = true; } }; if (window.ShowDialog() == true) { try { if (isOpened) { string sUrl = ConnectConfigData.UpLoadImage; HttpUploadFile(sUrl, fullFilename); } } catch (Exception ex) { MessageBox.Show("广告图片上传失败!", "提示信息"); return; } CurAdvert.Name = window.AdvertName; CurAdvert.Remark = window.Remark; CurAdvert.Status = window.State; if (isOpened) CurAdvert.Url = fileName; ErrType err = _businessService.EditAdvert(_loginID, CurAdvert); if (err.Err == ERR.SUCCESS) { MessageBox.Show("保存成功!", "提示信息", MessageBoxButton.OK, MessageBoxImage.Information); return; } else { MessageBox.Show(err.ErrMsg, err.ErrTitle, MessageBoxButton.OK, MessageBoxImage.Error); } } }