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

注:本例选自MSDN样例,并略有改动。
先看效果:
 
这里实现了以下几个功能:
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或互联网资源,如: http://www.brawdraw.com/userimages/aa.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 : 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));
            }
        }
    }
}
这里有两个公共属性:Directory和Path,用来获取或设置图像目录信息和路径,还有一个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();
        }
    }
}
这里有三个可读写属性: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) { }
    }
}

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中进行的)。

制作一个简单的WPF图片浏览器的更多相关文章

  1. 如何使用AEditor制作一个简单的H5交互页demo

    转载自:http://www.alloyteam.com/2015/06/h5-jiao-hu-ye-bian-ji-qi-aeditor-jie-shao/ 本教程演示如何使用AEditor制作一个 ...

  2. WPF图片浏览器(显示大图、小图等)

    原文:WPF图片浏览器(显示大图.小图等) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/wangshubo1989/article/details ...

  3. 实例学习SSIS(一)--制作一个简单的ETL包

    原文:实例学习SSIS(一)--制作一个简单的ETL包 导读: 实例学习SSIS(一)--制作一个简单的ETL包 实例学习SSIS(二)--使用迭代 实例学习SSIS(三)--使用包配置 实例学习SS ...

  4. WInform 创建一个简单的WPF应用

    (一)创建一个简单的WPF应用 首先,在这里我要说明的是:这里的例子,都是通过控制台程序来创建WPF应用,而非使用现成的WPF模版.因为WPF模版封装了创建WPF应用所需要的各种基本元素,并不利于我们 ...

  5. TensorFlow练习13: 制作一个简单的聊天机器人

    现在很多卖货公司都使用聊天机器人充当客服人员,许多科技巨头也纷纷推出各自的聊天助手,如苹果Siri.Google Now.Amazon Alexa.微软小冰等等.前不久有一个视频比较了Google N ...

  6. 手把手制作一个简单的IDEA插件(环境搭建Demo篇)

    新建IDEA插件File --> new --> Project--> Intellij PlatForm Plugin-->Next-->填好项目名OK 编写插件新建工 ...

  7. PureMVC和Unity3D的UGUI制作一个简单的员工管理系统实例

    前言: 1.关于PureMVC: MVC框架在很多项目当中拥有广泛的应用,很多时候做项目前人开坑开了一半就消失了,后人为了填补各种的坑就遭殃的不得了.嘛,程序猿大家都不喜欢像文案策划一样组织文字写东西 ...

  8. 自己制作一个简单的操作系统二[CherryOS]

    自己制作一个简单的操作系统二[CherryOS] 我的上一篇博客 自己制作一个简单的操作系统一[环境搭建], 详细介绍了制作所需的前期准备工作 一. 一点说明 这个操作系统只是第一步, 仅仅是开机显示 ...

  9. Expression Blend4经验分享:制作一个简单的图片按钮样式

    这次分享如何做一个简单的图片按钮经验 在我的个人Silverlight网页上,有个Iphone手机的效果,其中用到大量的图片按钮 http://raimon.6.gwidc.com/Iphone/de ...

随机推荐

  1. ORACLE RMAN备份及还原 RMAN能够进行增量备份:数据库,表空间,数据文件

    ORACLE RMAN备份及还原   RMAN能够进行增量备份:数据库.表空间.数据文件 仅仅有使用过的block能够被备份成backup set 表空间与数据文件相应关系:dba_data_file ...

  2. 8、hzk16的介绍以及简单的使用方法

    HZK16 字库是符合GB2312标准的16×16点阵字库,HZK16的GB2312-80支持的汉字有6763个,符号682个.其中一级汉字有3755个,按 声序排列,二级汉字有3008个,按偏旁部首 ...

  3. php实现数值的整数次方

    php实现数值的整数次方 一.总结 没有考虑到指数为负数的情况 二.php实现数值的整数次方 题目描述: 给定一个double类型的浮点数base和int类型的整数exponent.求base的exp ...

  4. php求和为s的两个数字(多复制上面写的代码,有利于检查错误)(由浅入深,先写简单算法,做题的话够用就行)

    php求和为s的两个数字(多复制上面写的代码,有利于检查错误)(由浅入深,先写简单算法,做题的话够用就行) 一.总结 1.多复制上面写的代码,有利于检查错误 2.一层循环就解决了,前后两个指针,和大了 ...

  5. [SCSS] Use Standard Built-in SCSS Functions for Common Operations

    We can use javascript for color and opacity variations, math, list and map logic or to see if someth ...

  6. [Javascript] Format console.log with CSS and String Template Tags

    The Chrome console allows you to format messages using CSS properties. This lesson walks you through ...

  7. ubuntu系统安装和配置

    1.分区信息 1.1 /boot分区 这个分区包括了操作系统的内核和在启动系统过程中所要用到的文件.假设有了一个单独的/boot启动分区,即使基本的根分区出现了问题,计算机依旧可以启动.这个分区的大小 ...

  8. MVC 设置项目默认起始页和多级目录的路由配置

    我们新建一个MVC的项目 默认的路由是这样的,但是由于一些需求,我们需要对Controllers按照一些规则分类. 比如说我们在Controllers下面建了一个School的文件夹,然后建了一个St ...

  9. 【hdu 2955】Robberies

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  10. 你的薪水增速跑赢GDP了没

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZm9ydW9r/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...