• 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.
  • 相关阅读:
    Java JVM启动参数
    使用Navicat连接MySQL8.0版本报1251错误
    安装MySQL和出现的问题解决
    跨域问题:解决跨域的三种方案
    Java8 新特性lambda表达式(一)初始
    搭建docker私有仓库
    crontab定时任务
    CentOS610 php环境安装
    Docker常用命令
    PHP调用python脚本执行时报错
  • 原文地址:https://www.cnblogs.com/wgscd/p/15083871.html
Copyright © 2020-2023  润新知