自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

教你制作一個(gè)簡單的WPF圖片瀏覽器

開發(fā) 后端
本文教你用.Net編程制作一個(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類,用來讀取像文件:

  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. 建立一個(gè)像列表的類,用于取得指定目錄下的所有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.     }  

  這里有兩個(gè)公共屬性:Directory和Path,用來獲取或設(shè)置像目錄信息和路徑,還有一個(gè)Update()私有方法,當(dāng)文件路徑變化時(shí),更新最新的像文件列表數(shù)據(jù)。

  3. 建立后期處理的類。

  由于后期加工均涉及“印”,所以就建立一個(gè)名為“印類型”(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.     }  

  這里有兩個(gè)只讀屬性:描述Description和費(fèi)用Cost,還對(duì)ToString()方法進(jìn)行了重載。

#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. 建立一個(gè)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.     }  

  這里有三個(gè)可讀寫屬性:Photo, PrintType和Quantity(表示片的數(shù)量),還設(shè)置了一個(gè)PropertyChanged委托,用于當(dāng)屬性變更時(shí)做相應(yīng)的事件處理。

  6. 繼承自PrintBase的三個(gè)類: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應(yīng)用成為一個(gè)灰色盒子
  3. 詳解.NET數(shù)組的前世今生
  4. .Net程序員以最簡單方式學(xué)習(xí)Linux
  5. WPF全視角學(xué)習(xí)指南
責(zé)任編輯:韓亞珊 來源: 飛諾網(wǎng)
相關(guān)推薦

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

瀏覽器平臺(tái)Chrome

2019-12-02 13:46:35

瀏覽器前端開發(fā)

2019-05-08 14:37:49

Web服務(wù)器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

開源瀏覽器操作系統(tǒng)

2021-08-06 16:52:10

瀏覽器HTTPS通信

2009-05-04 09:13:12

K-MeleonCCF瀏覽器

2016-09-21 12:34:10

Chrome瀏覽器插件

2023-12-21 11:12:31

Node.js.NET開源庫

2009-09-11 09:11:09

2013-06-14 17:16:44

WP開發(fā)Windows PhoWP應(yīng)用

2009-09-04 11:03:32

C#文件瀏覽器

2010-01-08 12:14:44

ibmdwAndroid

2010-04-05 21:57:14

Netscape瀏覽器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)