|
| 1 | +using Microsoft.Win32; |
| 2 | +using System; |
| 3 | +using System.IO; |
| 4 | +using System.Windows; |
| 5 | +using System.Windows.Controls; |
| 6 | +using System.Windows.Media.Imaging; |
| 7 | + |
| 8 | +namespace WPFDevelopers.Samples.ExampleViews |
| 9 | +{ |
| 10 | + public partial class CropImageExample : UserControl |
| 11 | + { |
| 12 | + public CropImageExample() |
| 13 | + { |
| 14 | + InitializeComponent(); |
| 15 | + } |
| 16 | + double ConvertBytesToMB(long bytes) |
| 17 | + { |
| 18 | + return (double)bytes / (1024 * 1024); |
| 19 | + } |
| 20 | + private void OnImportClickHandler(object sender, RoutedEventArgs e) |
| 21 | + { |
| 22 | + var openFileDialog = new OpenFileDialog(); |
| 23 | + openFileDialog.Filter = "图像文件(*.jpg;*.jpeg;*.png;)|*.jpg;*.jpeg;*.png;"; |
| 24 | + if (openFileDialog.ShowDialog() == true) |
| 25 | + { |
| 26 | + var fileInfo = new FileInfo(openFileDialog.FileName); |
| 27 | + var fileSize = fileInfo.Length; |
| 28 | + var mb = ConvertBytesToMB(fileSize); |
| 29 | + if (mb > 1) |
| 30 | + { |
| 31 | + WPFDevelopers.Controls.MessageBox.Show("图片不能大于 1M ", "提示", MessageBoxButton.OK, MessageBoxImage.Error); |
| 32 | + return; |
| 33 | + } |
| 34 | + var bitmap = new BitmapImage(); |
| 35 | + bitmap.BeginInit(); |
| 36 | + bitmap.CacheOption = BitmapCacheOption.OnLoad; |
| 37 | + bitmap.UriSource = new Uri(openFileDialog.FileName, UriKind.Absolute); |
| 38 | + bitmap.EndInit(); |
| 39 | + |
| 40 | + if (bitmap.PixelWidth > 500 || bitmap.PixelHeight > 500) |
| 41 | + { |
| 42 | + var width = (int)(bitmap.PixelWidth * 0.5); |
| 43 | + var height = (int)(bitmap.PixelHeight * 0.5); |
| 44 | + var croppedBitmap = new CroppedBitmap(bitmap, new Int32Rect(0, 0, width, height)); |
| 45 | + var bitmapNew = new BitmapImage(); |
| 46 | + bitmapNew.BeginInit(); |
| 47 | + bitmapNew.DecodePixelWidth = width; |
| 48 | + bitmapNew.DecodePixelHeight = height; |
| 49 | + var memoryStream = new MemoryStream(); |
| 50 | + var encoder = new JpegBitmapEncoder(); |
| 51 | + encoder.Frames.Add(BitmapFrame.Create(croppedBitmap.Source)); |
| 52 | + encoder.Save(memoryStream); |
| 53 | + memoryStream.Seek(0, SeekOrigin.Begin); |
| 54 | + bitmapNew.StreamSource = memoryStream; |
| 55 | + bitmapNew.EndInit(); |
| 56 | + MyCropImage.Source = bitmapNew; |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + MyCropImage.Source = bitmap; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + private void BtnSave_Click(object sender, RoutedEventArgs e) |
| 65 | + { |
| 66 | + var dlg = new SaveFileDialog(); |
| 67 | + dlg.FileName = $"WPFDevelopers_CropImage_{DateTime.Now.ToString("yyyyMMddHHmmss")}.jpg"; |
| 68 | + dlg.DefaultExt = ".jpg"; |
| 69 | + dlg.Filter = "image file|*.jpg"; |
| 70 | + if (dlg.ShowDialog() == true) |
| 71 | + { |
| 72 | + var pngEncoder = new PngBitmapEncoder(); |
| 73 | + pngEncoder.Frames.Add(BitmapFrame.Create((BitmapSource)MyCropImage.CurrentAreaBitmap)); |
| 74 | + using (var fs = File.OpenWrite(dlg.FileName)) |
| 75 | + { |
| 76 | + pngEncoder.Save(fs); |
| 77 | + fs.Dispose(); |
| 78 | + fs.Close(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments