• WPF 查找大小相同文件/图片


    假设文件大小一样就表示文件一模一样,是重复文件

    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>
    

      

    fffffffffffffffff
    test red font.
  • 相关阅读:
    System.Configuration引用后ConfigurationManager方法用不了
    HTTP 错误 500.23
    slide ——首尾相接の平滑切换效果
    函数式编程初探之回调
    Call & Apply. It's easy!
    【W3C】 CSS3选择器
    再谈原型和原型链
    ECMA学习小结(3)——constructor 和 prototype
    ECMA学习小结(2)——一切皆对象
    ECMA学习小结(1)——什么是ECMAScript,JAVAScript与ECMA的关系
  • 原文地址:https://www.cnblogs.com/wgscd/p/15083871.html
Copyright © 2020-2023  润新知