Repository navigation
Expand file tree
/
Copy pathCutImageExample.xaml.cs
More file actions
51 lines (48 loc) · 1.73 KB
/
Copy pathCutImageExample.xaml.cs
File metadata and controls
51 lines (48 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using Microsoft.Win32;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
namespace WPFDevelopers.Samples.ExampleViews
{
/// <summary>
/// CutImageExample.xaml 的交互逻辑
/// </summary>
public partial class CutImageExample : UserControl
{
public CutImageExample()
{
InitializeComponent();
this.btnImport.Click += OnImportClickHandler;
this.btnSave.Click += BtnSave_Click;
}
private void OnImportClickHandler(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
openFileDialog.Filter = "图像文件(*.jpg;*.jpeg;*.png;)|*.jpg;*.jpeg;*.png;";
if (openFileDialog.ShowDialog() == true)
{
cutCustoms.ImageSource = new BitmapImage(new Uri(openFileDialog.FileName));
btnSave.IsEnabled = true;
}
}
private void BtnSave_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.FileName = $"{DateTime.Now.ToString("yyyyMMddHHmmss")}.jpg";
dlg.DefaultExt = ".jpg";
dlg.Filter = "image file|*.jpg";
if (dlg.ShowDialog() == true)
{
BitmapEncoder pngEncoder = new PngBitmapEncoder();
pngEncoder.Frames.Add(BitmapFrame.Create((BitmapSource)image2.ImageSource));
using (var fs = System.IO.File.OpenWrite(dlg.FileName))
{
pngEncoder.Save(fs);
fs.Dispose();
fs.Close();
}
}
}
}
}