教你制作一个简单的WPF图片浏览器

开发 后端
本文教你用.Net编程制作一个简单的WPF图片浏览器,具体步骤详见下文。

  制作一个简单的WPF图片浏览器  

  这里将实现以下几个功能:

  1. 对指定文件夹下所有JPG文件进行预览

  2. 对选定片进行旋转

  3. 对选定片进行灰度处理

  4. 对选定片进行裁切处理

  5. 无限制的恢复功能

  6. 类似加入购物车的功能

  以下来看看其实现过程。

  1. 建立一个ImageFile类,用来读取像文件:

  1. // ImageFile.cs  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Windows.Media.Imaging;  
  5. using System.Text;  
  6.  
  7. namespace PhotoDemo  
  8. {  
  9.     public class ImageFile  
  10.     {  
  11.         private String _path;  
  12.         public String Path { get { return _path; } }  
  13.  
  14.         private Uri _uri;  
  15.         public Uri Uri { get { return _uri; } }  
  16.  
  17.         private BitmapFrame _image;  
  18.         public BitmapFrame Image { get { return _image; } }  
  19.  
  20.         public ImageFile(string path)  
  21.         {  
  22.             _path = path;  
  23.             _uri = new Uri(_path);  
  24.             _image = BitmapFrame.Create(_uri);  
  25.         }  
  26.  
  27.         public override string ToString()  
  28.         {  
  29.             return Path;  
  30.         }  
  31.     }  

  2. 建立一个像列表的类,用于取得指定目录下的所有jpg像文件

  1. // PhotoList.cs  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Collections.ObjectModel;  
  5. using System.IO;  
  6. using System.Text;  
  7.  
  8. namespace PhotoDemo  
  9. {  
  10.     public class PhotoList : ObservableCollection<ImageFile> 
  11.     {  
  12.         DirectoryInfo _directory;  
  13.         public DirectoryInfo Directory  
  14.         {  
  15.             set  
  16.             {  
  17.                 _directory = value;  
  18.                 Update();  
  19.             }  
  20.             get { return _directory; }  
  21.         }  
  22.  
  23.         public string Path  
  24.         {  
  25.             set  
  26.             {  
  27.                 _directory = new DirectoryInfo(value);  
  28.                 Update();  
  29.             }  
  30.             get { return _directory.FullName; }  
  31.         }  
  32.  
  33.         public PhotoList() { }  
  34.  
  35.         public PhotoList(DirectoryInfo directory)  
  36.         {  
  37.             _directory = directory;  
  38.             Update();  
  39.         }  
  40.  
  41.         public PhotoList(string path) : this(new DirectoryInfo(path)) { }  
  42.  
  43.         private void Update()  
  44.         {  
  45.             foreach (FileInfo f in _directory.GetFiles("*.jpg"))  
  46.             {  
  47.                 Add(new ImageFile(f.FullName));  
  48.             }  
  49.         }  
  50.     }  

  这里有两个公共属性:Directory和Path,用来获取或设置像目录信息和路径,还有一个Update()私有方法,当文件路径变化时,更新最新的像文件列表数据。

  3. 建立后期处理的类。

  由于后期加工均涉及“印”,所以就建立一个名为“印类型”(PrintType)的类:

  1. // PrintType.cs  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Text;  
  5.  
  6. namespace PhotoDemo  
  7. {  
  8.     public class PrintType  
  9.     {  
  10.         private string _description;  
  11.         public string Description { get { return _description; } }  
  12.  
  13.         private double _cost;  
  14.         public double Cost { get { return _cost; } }  
  15.  
  16.         public PrintType(string description, double cost)  
  17.         {  
  18.             _description = description;  
  19.             _cost = cost;  
  20.         }  
  21.  
  22.       public override string ToString()  
  23.         {  
  24.             return _description;  
  25.         }  
  26.     }  

  这里有两个只读属性:描述Description和费用Cost,还对ToString()方法进行了重载。

#p#

  4. PrintTypeList类,是PrintType列表的集合

  1. // PrintTypeList .cs  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Collections.ObjectModel;  
  5. using System.Text;  
  6.  
  7.    
  8. namespace PhotoDemo  
  9. {  
  10.     public class PrintTypeList : ObservableCollection<PrintType> 
  11.     {  
  12.         public PrintTypeList()  
  13.         {  
  14.             Add(new PrintType("4x6 Print", 0.15));  
  15.             Add(new PrintType("Greeting Card", 1.49));  
  16.             Add(new PrintType("T-Shirt", 14.99));  
  17.         }  
  18.     }  

  5. 建立一个PrintBase的类

  1. // PrintBase.cs  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.ComponentModel;  
  5. using System.Windows.Media.Imaging;  
  6. using System.Text;  
  7.  
  8. namespace PhotoDemo  
  9. {  
  10.     public class PrintBase : INotifyPropertyChanged  
  11.     {  
  12.         #region public property  
  13.         private BitmapSource _photo;  
  14.         public BitmapSource Photo  
  15.         {  
  16.             set { _photo = value; OnPropertyChanged("Photo"); }  
  17.             get { return _photo; }  
  18.         }  
  19.  
  20.         private PrintType _PrintType;  
  21.         public PrintType PrintType  
  22.         {  
  23.             set { _PrintType = value; OnPropertyChanged("PrintType"); }  
  24.             get { return _PrintType; }  
  25.         }  
  26.  
  27.         private int _quantity;  
  28.         public int Quantity  
  29.         {  
  30.             set { _quantity = value; OnPropertyChanged("Quantity"); }  
  31.             get { return _quantity; }  
  32.         }  
  33.         #endregion public property  
  34.  
  35.         public PrintBase(BitmapSource photo, PrintType printtype, int quantity)  
  36.         {  
  37.             Photo = photo;  
  38.             PrintType = printtype;  
  39.             Quantity = quantity;  
  40.         }  
  41.  
  42.         public PrintBase(BitmapSource photo, string description, double cost)  
  43.         {  
  44.             Photo = photo;  
  45.             PrintType = new PrintType(description, cost);  
  46.             Quantity = 0;  
  47.         }  
  48.  
  49.         public event PropertyChangedEventHandler PropertyChanged;  
  50.         private void OnPropertyChanged(String info)  
  51.         {  
  52.             if (PropertyChanged != null)  
  53.                 PropertyChanged(this, new PropertyChangedEventArgs(info));  
  54.         }  
  55.  
  56.         public override string ToString()  
  57.         {  
  58.             return PrintType.ToString();  
  59.         }  
  60.     }  

  这里有三个可读写属性:Photo, PrintType和Quantity(表示片的数量),还设置了一个PropertyChanged委托,用于当属性变更时做相应的事件处理。

  6. 继承自PrintBase的三个类:Print, GreetingCard, TShirt, 分别用来打印,制成贺卡及制作T恤衫。

  1. // Print.cs  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Windows.Media.Imaging;  
  5. using System.Text;  
  6.  
  7. namespace PhotoDemo  
  8. {  
  9.     public class Print : PrintBase  
  10.     {  
  11.         public Print(BitmapSource photo) : base(photo, "4x6 Print", 0.15) { }  
  12.     }  
  13. }  
  14.  
  15. // TShirt.cs  
  16. using System;  
  17. using System.Collections.Generic;  
  18. using System.Windows.Media.Imaging;  
  19. using System.Text;  
  20.  
  21. namespace PhotoDemo  
  22. {  
  23.     public class TShirt : PrintBase  
  24.     {  
  25.         public TShirt(BitmapSource photo) : base(photo, "T-Shirt", 14.99) { }  
  26.     }  
  27. }  
  28.  
  29. // GreetingCard.cs  
  30. using System;  
  31. using System.Collections.Generic;  
  32. using System.Windows.Media.Imaging;  
  33. using System.Text;  
  34.  
  35. namespace PhotoDemo  
  36. {  
  37.     public class GreetingCard : PrintBase  
  38.     {  
  39.         public GreetingCard(BitmapSource photo) : base(photo, "Greeting Card", 1.49) { }  
  40.     }  

  7. "印"的集合:PrintList

  1. // PrintList.cs  
  2. using System;  
  3. using System.Collections.ObjectModel;  
  4.  
  5. namespace PhotoDemo  
  6. {  
  7.     public class PrintList : ObservableCollection<PrintBase> { }  

【编辑推荐】

  1. 深入浅出WPF
  2. 让你的.NET应用成为一个灰色盒子
  3. 详解.NET数组的前世今生
  4. .Net程序员以最简单方式学习Linux
  5. WPF全视角学习指南
责任编辑:韩亚珊 来源: 飞诺网
相关推荐

2022-06-13 06:33:04

浏览器浏览器插件

2012-04-25 14:06:45

HTML5

2017-12-14 15:45:02

2012-09-03 10:24:16

果粉浏览器

2009-05-27 08:54:15

浏览器平台Chrome

2019-12-02 13:46:35

浏览器前端开发

2021-06-02 06:14:50

Nyxt浏览器

2022-06-20 09:01:56

Plasmo开源

2019-05-08 14:37:49

Web服务器HTTP

2014-08-18 14:58:25

微软IE

2020-07-06 08:23:11

开源浏览器操作系统

2021-08-06 16:52:10

浏览器HTTPS通信

2009-05-04 09:13:12

K-MeleonCCF浏览器

2023-12-21 11:12:31

Node.js.NET开源库

2016-09-21 12:34:10

Chrome浏览器插件

2013-06-14 17:16:44

WP开发Windows PhoWP应用

2010-01-08 12:14:44

ibmdwAndroid

2009-09-04 11:03:32

C#文件浏览器

2015-03-24 13:39:08

IE

2020-10-22 19:37:28

360浏览器浏览器
点赞
收藏

51CTO技术栈公众号