這裡實現了以下幾個功能:
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;
}
}
}
這裡有三個只讀屬性:Path, Uri和BitmapFrame,分別表示圖像圖路,通用資源標志符(可以是本地,如c:\myimage\aa.jpg或互聯網資源,如: /School/UploadFiles_7810/201104/20110418180910650.jpg 等)及圖像。BitmapFrame是使用編碼、解碼器返回或獲取圖像數據的類,類似於GDI+中的Bitmap類。
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 :
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()
{
由於需要對圖片做後期處理(如沖/打印,制作成卡片或加工成為T恤衫等),要加入一個類似購物車之類的計算功能(實際的計算功能等未作實現),因此還需要:
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;
}
}
}
這裡有兩個只讀屬性:描述Description和費用Cost,還對ToString()方法進行了重載。
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. 建立一個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();
}
}
}
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) { }
}
}
7. "印"的集合:PrintList
// PrintList.cs
using System;
using System.Collections.ObjectModel;
namespace PhotoDemo
{
public class PrintList : ObservableCollection<PrintBase> { }
}
8. 還有就是用於裁切操作的類,由於內容較多,改在下一篇“利用Adorner制作用於圖像裁切的選擇框”中繼續。
9. 窗口主程序的編寫,其中包括XAML代碼與C#代碼。由於內容較多,放在另一篇中詳解。
10. 程序的啟動、配置等(在WPF中,一般都是app.xml, app.XML.cs中進行的)。