教你制作一個(gè)簡單的WPF圖片瀏覽器
制作一個(gè)簡單的WPF圖片瀏覽器
這里將實(shí)現(xiàn)以下幾個(gè)功能:
1. 對(duì)指定文件夾下所有JPG文件進(jìn)行預(yù)覽
2. 對(duì)選定片進(jìn)行旋轉(zhuǎn)
3. 對(duì)選定片進(jìn)行灰度處理
4. 對(duì)選定片進(jìn)行裁切處理
5. 無限制的恢復(fù)功能
6. 類似加入購物車的功能
以下來看看其實(shí)現(xiàn)過程。
1. 建立一個(gè)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;
- }
- }
- }
2. 建立一個(gè)像列表的類,用于取得指定目錄下的所有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));
- }
- }
- }
- }
這里有兩個(gè)公共屬性:Directory和Path,用來獲取或設(shè)置像目錄信息和路徑,還有一個(gè)Update()私有方法,當(dāng)文件路徑變化時(shí),更新最新的像文件列表數(shù)據(jù)。
3. 建立后期處理的類。
由于后期加工均涉及“印”,所以就建立一個(gè)名為“印類型”(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;
- }
- }
- }
這里有兩個(gè)只讀屬性:描述Description和費(fèi)用Cost,還對(duì)ToString()方法進(jìn)行了重載。
#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));
- }
- }
- }
5. 建立一個(gè)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();
- }
- }
- }
這里有三個(gè)可讀寫屬性:Photo, PrintType和Quantity(表示片的數(shù)量),還設(shè)置了一個(gè)PropertyChanged委托,用于當(dāng)屬性變更時(shí)做相應(yīng)的事件處理。
6. 繼承自PrintBase的三個(gè)類: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) { }
- }
- }
7. "印"的集合:PrintList
- // PrintList.cs
- using System;
- using System.Collections.ObjectModel;
- namespace PhotoDemo
- {
- public class PrintList : ObservableCollection<PrintBase> { }
- }
【編輯推薦】