假设文件大小一样就表示文件一模一样,是重复文件
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.Shapes;
using System.IO;
namespace EnhanceImage
{
public partial class FindSamePicWind : Window
{
public FindSamePicWind()
{
InitializeComponent();
List<FileInfo> find = new List<FileInfo>();
//d:Downloads
string path = @"d:Downloads";
var files = Directory.GetFiles(path, "*.jpg");
var list = new List<FileInfo>();
foreach (var f in files)
{
FileInfo fi = new FileInfo(f);
list.Add(fi);
}
foreach (var f in list)
{
var all = list.Where(n => n.Length == f.Length);
if (all.Count() > 1)
{
foreach (var item in all)
{
if (!find.Contains(item))
{
find.Add(item);
Console.WriteLine(f.FullName);
var img = new Image() { ToolTip=f.Name, Tag = f.FullName, MaxWidth = 200, Source = LoadImage(f.FullName) };
img.MouseLeftButtonDown += Img_MouseLeftButtonDown;
wrapPanel.Children.Add( img );
}
}
}
}
}
private void Img_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var f = "" + (sender as Image).Tag;
File.Delete(f);
wrapPanel.Children.Remove((sender as Image));
}
BitmapImage LoadImage(string f) {
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.CacheOption = BitmapCacheOption.OnLoad;
using(MemoryStream ms=new MemoryStream(File.ReadAllBytes(f)))
{
ms.Position = 0;
bmp.StreamSource = ms;// new Uri(f,UriKind.RelativeOrAbsolute);
bmp.EndInit();
bmp.Freeze();
}
return bmp;
}
}
}
XAML:
<Window x:Class="EnhanceImage.FindSamePicWind"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:EnhanceImage"
mc:Ignorable="d"
Title="FindSamePicWind" Height="450" Width="800">
<Grid>
<RichTextBox AcceptsReturn="True" x:Name="txt"></RichTextBox>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<WrapPanel Name="wrapPanel"></WrapPanel>
</ScrollViewer>
</Grid>
</Window>