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

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

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

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

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

  2. 对选定片进行旋转

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

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

  5. 无限制的恢复功能

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

  以下来看看其实现过程。

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

// ImageFile.cs  
using System;  
using System.Collections.Generic;  
using System.Windows.Media.Imaging;  
using System.Text;  
 
namespace PhotoDemo  
{  
    public class ImageFile  
    {  
        private String _path;  
        public String Path { get { return _path; } }  
 
        private Uri _uri;  
        public Uri Uri { get { return _uri; } }  
 
        private BitmapFrame _image;  
        public BitmapFrame Image { get { return _image; } }  
 
        public ImageFile(string path)  
        {  
            _path = path;  
            _uri = new Uri(_path);  
            _image = BitmapFrame.Create(_uri);  
        }  
 
        public override string ToString()  
        {  
            return Path;  
        }  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.

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

// PhotoList.cs  
using System;  
using System.Collections.Generic;  
using System.Collections.ObjectModel;  
using System.IO;  
using System.Text;  
 
namespace PhotoDemo  
{  
    public class PhotoList : ObservableCollection<ImageFile> 
    {  
        DirectoryInfo _directory;  
        public DirectoryInfo Directory  
        {  
            set  
            {  
                _directory = value;  
                Update();  
            }  
            get { return _directory; }  
        }  
 
        public string Path  
        {  
            set  
            {  
                _directory = new DirectoryInfo(value);  
                Update();  
            }  
            get { return _directory.FullName; }  
        }  
 
        public PhotoList() { }  
 
        public PhotoList(DirectoryInfo directory)  
        {  
            _directory = directory;  
            Update();  
        }  
 
        public PhotoList(string path) : this(new DirectoryInfo(path)) { }  
 
        private void Update()  
        {  
            foreach (FileInfo f in _directory.GetFiles("*.jpg"))  
            {  
                Add(new ImageFile(f.FullName));  
            }  
        }  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.

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

  3. 建立后期处理的类。

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

// PrintType.cs  
using System;  
using System.Collections.Generic;  
using System.Text;  
 
namespace PhotoDemo  
{  
    public class PrintType  
    {  
        private string _description;  
        public string Description { get { return _description; } }  
 
        private double _cost;  
        public double Cost { get { return _cost; } }  
 
        public PrintType(string description, double cost)  
        {  
            _description = description;  
            _cost = cost;  
        }  
 
      public override string ToString()  
        {  
            return _description;  
        }  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

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

#p#

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

// PrintTypeList .cs  
using System;  
using System.Collections.Generic;  
using System.Collections.ObjectModel;  
using System.Text;  
 
   
namespace PhotoDemo  
{  
    public class PrintTypeList : ObservableCollection<PrintType> 
    {  
        public PrintTypeList()  
        {  
            Add(new PrintType("4x6 Print", 0.15));  
            Add(new PrintType("Greeting Card", 1.49));  
            Add(new PrintType("T-Shirt", 14.99));  
        }  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

  5. 建立一个PrintBase的类

// PrintBase.cs  
using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Windows.Media.Imaging;  
using System.Text;  
 
namespace PhotoDemo  
{  
    public class PrintBase : INotifyPropertyChanged  
    {  
        #region public property  
        private BitmapSource _photo;  
        public BitmapSource Photo  
        {  
            set { _photo = value; OnPropertyChanged("Photo"); }  
            get { return _photo; }  
        }  
 
        private PrintType _PrintType;  
        public PrintType PrintType  
        {  
            set { _PrintType = value; OnPropertyChanged("PrintType"); }  
            get { return _PrintType; }  
        }  
 
        private int _quantity;  
        public int Quantity  
        {  
            set { _quantity = value; OnPropertyChanged("Quantity"); }  
            get { return _quantity; }  
        }  
        #endregion public property  
 
        public PrintBase(BitmapSource photo, PrintType printtype, int quantity)  
        {  
            Photo = photo;  
            PrintType = printtype;  
            Quantity = quantity;  
        }  
 
        public PrintBase(BitmapSource photo, string description, double cost)  
        {  
            Photo = photo;  
            PrintType = new PrintType(description, cost);  
            Quantity = 0;  
        }  
 
        public event PropertyChangedEventHandler PropertyChanged;  
        private void OnPropertyChanged(String info)  
        {  
            if (PropertyChanged != null)  
                PropertyChanged(this, new PropertyChangedEventArgs(info));  
        }  
 
        public override string ToString()  
        {  
            return PrintType.ToString();  
        }  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.

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

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

// Print.cs  
using System;  
using System.Collections.Generic;  
using System.Windows.Media.Imaging;  
using System.Text;  
 
namespace PhotoDemo  
{  
    public class Print : PrintBase  
    {  
        public Print(BitmapSource photo) : base(photo, "4x6 Print", 0.15) { }  
    }  
}  
 
// TShirt.cs  
using System;  
using System.Collections.Generic;  
using System.Windows.Media.Imaging;  
using System.Text;  
 
namespace PhotoDemo  
{  
    public class TShirt : PrintBase  
    {  
        public TShirt(BitmapSource photo) : base(photo, "T-Shirt", 14.99) { }  
    }  
}  
 
// GreetingCard.cs  
using System;  
using System.Collections.Generic;  
using System.Windows.Media.Imaging;  
using System.Text;  
 
namespace PhotoDemo  
{  
    public class GreetingCard : PrintBase  
    {  
        public GreetingCard(BitmapSource photo) : base(photo, "Greeting Card", 1.49) { }  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.

  7. "印"的集合:PrintList

// PrintList.cs  
using System;  
using System.Collections.ObjectModel;  
 
namespace PhotoDemo  
{  
    public class PrintList : ObservableCollection<PrintBase> { }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

【编辑推荐】

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

2022-06-13 06:33:04

浏览器浏览器插件

2017-12-14 15:45:02

2012-04-25 14:06:45

HTML5

2012-09-03 10:24:16

果粉浏览器

2009-05-27 08:54:15

浏览器平台Chrome

2019-12-02 13:46:35

浏览器前端开发

2019-05-08 14:37:49

Web服务器HTTP

2021-06-02 06:14:50

Nyxt浏览器

2022-06-20 09:01:56

Plasmo开源

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浏览器插件

2009-09-11 09:11:09

2013-06-14 17:16:44

WP开发Windows PhoWP应用

2009-09-04 11:03:32

C#文件浏览器

2010-01-08 12:14:44

ibmdwAndroid

2020-10-22 19:37:28

360浏览器浏览器
点赞
收藏

51CTO技术栈公众号