using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;
using System.IO;
using System.Drawing;
using Microsoft.Win32;
namespace SegImgs
{
enum EnumAlgoType
{
CvCoreDll,
CvCoreDll_2,
}
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
private List _datas = new List();
private string _origImgFolder;
private string _dstImgFolder;
private List _imgs = new List();
private Queue _foldersToSearch = new Queue();
private BitmapImage _imgorig;
private BitmapImage _imgcrop;
public delegate void deleMethod();
public MainWindow()
{
InitializeComponent();
_imgorig = null;
_imgcrop = null;
ImgOrig.Source = _imgorig;
ImgCrop.Source = _imgcrop;
}
private void CropImgsInFolderClicked(object sender, RoutedEventArgs e)
{
try
{
System.Windows.Forms.FolderBrowserDialog origFolderD = new System.Windows.Forms.FolderBrowserDialog();
origFolderD.Description = "请选择需要批量裁图的文件夹";
if (origFolderD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (string.IsNullOrEmpty(origFolderD.SelectedPath))
{
MessageBox.Show("待裁切的文件夹不能为空!");
return;
}
_origImgFolder = origFolderD.SelectedPath;
System.Windows.Forms.FolderBrowserDialog dstFolderD = new System.Windows.Forms.FolderBrowserDialog();
dstFolderD.Description = "请选择保存裁切后图像的文件夹";
if (dstFolderD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (string.IsNullOrEmpty(dstFolderD.SelectedPath))
{
MessageBox.Show("保存裁切后图像的文件夹不能为空!");
return;
}
_dstImgFolder = dstFolderD.SelectedPath;
EnumAlgoType algo = EnumAlgoType.CvCoreDll;
if (RadioBtnAlgoOpenCV.IsChecked ?? false)
{
algo = EnumAlgoType.CvCoreDll;
}
else if(RadioBtnAlgoOpenCVAnother.IsChecked ?? false)
{
algo = EnumAlgoType.CvCoreDll_2;
}
DoBatchCrop(algo);
}
}
}
catch (Exception excep)
{
MessageBox.Show("批量裁切文件夹中的图片时发生错误:" + excep.Message);
}
}
private void CropOneImgClicked(object sender, RoutedEventArgs e)
{
try
{
OpenFileDialog openFileD = new OpenFileDialog();
openFileD.Filter = "图片文件|*.png;*.bmp;*.jpg;*.jpeg;*.tiff";
openFileD.Multiselect = false;
openFileD.Title = "选择图像";
if (openFileD.ShowDialog() ?? false)
{
Bitmap img = new Bitmap(openFileD.FileName, true);
_imgorig = img.BitmapToBitmapImage();
_imgcrop = null;
ImgOrig.Source = _imgorig;
ImgCrop.Source = _imgcrop;
System.Drawing.Rectangle rect = System.Drawing.Rectangle.Empty;
EnumAlgoType algo = EnumAlgoType.CvCoreDll;
if (RadioBtnAlgoOpenCV.IsChecked ?? false)
{
algo = EnumAlgoType.CvCoreDll;
}
else if (RadioBtnAlgoOpenCVAnother.IsChecked ?? false)
{
algo = EnumAlgoType.CvCoreDll_2;
}
if (algo == EnumAlgoType.CvCoreDll)
{
if (!img.CropWithCVCore(out rect))
{
img.Dispose();
MessageBox.Show("裁切失败");
return;
}
}
if (algo == EnumAlgoType.CvCoreDll_2)
{
if (!img.CropWithCvAlgorithm2(out rect))
{
img.Dispose();
MessageBox.Show("裁切失败");
return;
}
}
Bitmap dstimg = new Bitmap(rect.Width, rect.Height, img.PixelFormat);
using (var g = Graphics.FromImage(dstimg))
{
g.DrawImage(img, new System.Drawing.Rectangle(0, 0, rect.Width, rect.Height),
new System.Drawing.Rectangle(rect.Left, rect.Top, rect.Width, rect.Height),
GraphicsUnit.Pixel);
g.Dispose();
}
_imgcrop = dstimg.BitmapToBitmapImage();
ImgCrop.Source = _imgcrop;
dstimg.Dispose();
img.Dispose();
}
}
catch (Exception excep)
{
MessageBox.Show("裁切一幅图时发生错误:" + excep.Message);
}
}
private void DoBatchCrop(EnumAlgoType algoType)
{
ThreadPool.QueueUserWorkItem((e) =>
{
DirectoryInfo folder = new DirectoryInfo(_origImgFolder);
foreach (FileInfo file in folder.GetFiles())
{
if (file.Extension == ".png" || file.Extension == ".jpg" || file.Extension == ".bmp" || file.Extension == ".tiff")
{
Bitmap img = new Bitmap(file.FullName, true);
_imgorig = img.BitmapToBitmapImage();
_imgcrop = null;
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
ImgOrig.Source = _imgorig;
ImgCrop.Source = _imgcrop;
}));
System.Drawing.Rectangle rect = System.Drawing.Rectangle.Empty;
if (algoType == EnumAlgoType.CvCoreDll)
{
if (!img.CropWithCVCore(out rect))
{
img.Dispose();
continue;
}
}
if(algoType == EnumAlgoType.CvCoreDll_2)
{
if (!img.CropWithCvAlgorithm2(out rect))
{
img.Dispose();
continue;
}
}
Bitmap dstimg = new Bitmap(rect.Width, rect.Height, img.PixelFormat);
using (var g = Graphics.FromImage(dstimg))
{
g.DrawImage(img, new System.Drawing.Rectangle(0, 0, rect.Width, rect.Height),
new System.Drawing.Rectangle(rect.Left, rect.Top, rect.Width, rect.Height),
GraphicsUnit.Pixel);
g.Dispose();
}
dstimg.Save(_dstImgFolder + "\\" + System.IO.Path.GetFileNameWithoutExtension(file.Name) +".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
_imgcrop = dstimg.BitmapToBitmapImage();
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
ImgCrop.Source = _imgcrop;
}));
dstimg.Dispose();
img.Dispose();
}
}
MessageBox.Show("裁切完毕!");
});
}
}
}