创建一个新工程

1、在 VS 上,选择 File > New > Project..

2、在新工程窗口,选择  Visual C# > Windows Store > Blank App(XAML)

3、点击 OK.

向工程中添加类库

在使用 Nokia Imaging SDK 类库中的功能时,首先需要添加相应类库,请参考

Adding libraries to the project.

移除 Any CPU platform 选项

Nokia Imaging SDK 支持 ARM 和 X86 平台,不支持 Any CPU 。在编译引用了 Nokia Imaging SDK

的工程前,你必须移除  Any CPU 选项。

1、在 VS 工具栏,选择 Solution Configuration

2、选择 Configuration Manager...

3、从 Platform 中选择  <Edit...> 然后点击 Any   CPU

4、点击 Remove

定义你的 XAML UI

这里定义的 UI 很简单,只有两个 XAML image 控件和两个 按钮。一个 Image 控件显示

原图,另一个显示经过滤镜处理的 Image。另外,一个按钮用来选择图片,另一个保存图片。

实现步骤

用下面的 XAML 替换 MainPage.xaml 中的 Grid:

<Grid Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions> <Image x:Name="CartoonImage" Stretch="UniformToFill" Grid.RowSpan="3" />
<Image x:Name="OriginalImage" Width="334" Height="200" Stretch="UniformToFill"
Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="12,12,0,0" /> <TextBlock Text="Nokia Imaging SDK" Grid.Row="0" Style="{StaticResource TitleTextBlockStyle}" /> <Button Content="Pick an image" Click="PickImage_Click" HorizontalAlignment="Left" Grid.Row="2" />
<Button Content="Save the image" Click="SaveImage_Click" x:Name="SaveButton" HorizontalAlignment="Right"
Grid.Row="2" IsEnabled="False" /> </Grid>

从 Camera Roll 中选择图片

接下来在  MainPage.xaml.cs 中使用 Win RT 库中提供的 FileOpenPicker 选择图片:

private async void PickImage_Click(object sender, RoutedEventArgs e)
{
SaveButton.IsEnabled = false; var openPicker = new FileOpenPicker
{
SuggestedStartLocation = PickerLocationId.PicturesLibrary,
ViewMode = PickerViewMode.Thumbnail
}; // Filter to include a sample subset of file types.
openPicker.FileTypeFilter.Clear();
openPicker.FileTypeFilter.Add(".bmp");
openPicker.FileTypeFilter.Add(".png");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".jpg"); // Open the file picker.
StorageFile file = await openPicker.PickSingleFileAsync(); // file is null if user cancels the file picker.
if (file != null)
{
if (await ApplyFilter(file)) return; SaveButton.IsEnabled = true;
}
} private async Task<bool> ApplyFilter(StorageFile file)
{
IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read); return false;
}

当用户选择了图片,我们通过 IRandomAccessStream 来打开文件。

使用 FilterEffect 来为图片添加一个 effect

1、添加下面的命名空间,来自 SDK

using Nokia.Graphics.Imaging;
using Windows.UI.Xaml.Media.Imaging;

2、下面的命名空间用来 “选择”  和 “保存” 图片

using Windows.Storage.Streams;
using Windows.Storage.Pickers;
using Windows.Storage;

3、在 MainPage.xaml.cs 中,定义以下成员变量:

public sealed partial class MainPage : Page
{
// FilterEffect instance is used to apply different
// filters to an image.
// Here we will apply Cartoon filter to an image.
private FilterEffect _cartoonEffect; // The following WriteableBitmap contains
// The filtered and thumbnail image.
private WriteableBitmap _cartoonImageBitmap;
private WriteableBitmap _thumbnailImageBitmap;

4、然后在构造函数中,初始化这些变量:

public MainPage()
{
InitializeComponent(); Rect bounds = Window.Current.Bounds; _cartoonImageBitmap = new WriteableBitmap((int)bounds.Width, (int)bounds.Height);
_thumbnailImageBitmap = new WriteableBitmap((int)OriginalImage.Width, (int)OriginalImage.Height);
}

5、当用户选择完图片,我们就为图片添加滤镜。在这里我们添加 Cartoon 滤镜,然后把结果显示到 XAML

上的 Image 控件(CartoonImage):

private async Task<bool> ApplyFilter(StorageFile file)
{
// Open a stream for the selected file.
IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read); try
{
// Show thumbnail of original image.
_thumbnailImageBitmap.SetSource(fileStream);
OriginalImage.Source = _thumbnailImageBitmap; // Rewind stream to start.
fileStream.Seek(); // A cartoon effect is initialized with selected image stream as source.
var imageStream = new RandomAccessStreamImageSource(fileStream);
_cartoonEffect = new FilterEffect(imageStream); // Add the cartoon filter as the only filter for the effect.
var cartoonFilter = new CartoonFilter();
_cartoonEffect.Filters = new[] {cartoonFilter}; // Render the image to a WriteableBitmap.
var renderer = new WriteableBitmapRenderer(_cartoonEffect, _cartoonImageBitmap);
_cartoonImageBitmap = await renderer.RenderAsync(); // Set the rendered image as source for the cartoon image control.
CartoonImage.Source = _cartoonImageBitmap;
}
catch (Exception exception)
{
var dia = new MessageDialog(exception.Message);
dia.ShowAsync();
return true;
}
return false;
}

添加相应的权限

因为应用程序需要从 Pictures 文件夹读取数据,所以需要添加相应的权限:

1、在 VS 解决方案中,打开package.appxmanifest

2、选择 Capabilities  选项卡,然后选择Pictures Library,如下图:

生成和编码全尺寸 JPEG 图片

使用 FilterEffect 类的对象来输出全尺寸 JPEG 图片是很简单的。原图的每一个像素都会被

处理并且输出 JPEG 格式。

下面的示例演示如何把滤镜处理后的全尺寸图片保存到文件:

private async void SaveImage_Click(object sender, RoutedEventArgs e)
{
SaveButton.IsEnabled = false; if (_cartoonEffect == null)
{
return;
} var jpegRenderer = new JpegRenderer(_cartoonEffect); // Jpeg renderer gives the raw buffer for the filtered image.
IBuffer jpegOutput = await jpegRenderer.RenderAsync(); var picker = new FileSavePicker();
picker.FileTypeChoices.Add("JPG File", new List<string> { ".jpg" });
picker.SuggestedFileName = string.Format("CartoonImage_{0:G}", DateTime.Now);
StorageFile file = await picker.PickSaveFileAsync(); if (file != null)
{
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
await stream.WriteAsync(jpegOutput);
await stream.FlushAsync();
}
} SaveButton.IsEnabled = true;
}

获取完整源码:点击获取

原文链接

04、Quick Start for Windows的更多相关文章

  1. 02、Quick Start for Windows phone

    在使用这个 SDK 提供的功能前,必须先添加类库的引用到你的工程里.参考: Download and add the libraries to the project. 定义你的 XAML 的 UI ...

  2. Ubuntu14.04、win7双系统如何设置win7为默认启动项

    Ubuntu14.04.win7双系统如何设置win7为默认启动项 Ubuntu14.04.win7双系统设置win7为默认启动项方法: 在启动项选择菜单处记住windows 7对应的序号. 从上至下 ...

  3. 配置 Docker 加速器:适用于 Ubuntu14.04、Debian、CentOS6 、CentOS7、Fedora、Arch Linux、openSUSE Leap 42.1

    天下容器, 唯快不破 Docker Hub 提供众多镜像,你可以从中自由下载数十万计的免费应用镜像, 这些镜像作为 docker 生态圈的基石,是我们使用和学习 docker 不可或缺的资源.为了解决 ...

  4. 利用ArcGIS Engine、VS .NET和Windows控件开发GIS应用

    Dixon 原文  用ArcGIS Engine.VS .NET和Windows控件开发GIS应用     此过程说明适合那些使用.NET建立和部署应用的开发者,它描述了使用ArcGIS控件建立和部署 ...

  5. 5 weekend01、02、03、04、05、06、07的分布式集群的HA测试 + hdfs--动态增加节点和副本数量管理 + HA的java api访问要点

    weekend01.02.03.04.05.06.07的分布式集群的HA测试 1)  weekend01.02的hdfs的HA测试 2)  weekend03.04的yarn的HA测试 1)  wee ...

  6. SVN二次开发——让SVN、TSVN(TortoiseSVN)支持windows的访问控制模型、NTFS ADS(可选数据流、NTFS的安全属性)

    SVN二次开发 ——让SVN.TSVN(TortoiseSVN)支持windows的访问控制模型.NTFS ADS (可选数据流.NTFS的安全属性) SVN secondary developmen ...

  7. 04、NetCore2.0下Web应用之Startup源码解析

    04.NetCore2.0Web应用之Startup源码解析   通过分析Asp.Net Core 2.0的Startup部分源码,来理解插件框架的运行机制,以及掌握Startup注册的最优姿势. - ...

  8. SQL Server ->> 高可用与灾难恢复(HADR)技术 -- AlwaysOn(实战篇)之建立活动目录域、DNS服务器和Windows故障转移群集(准备工作)

    因为篇幅原因,AlwaysOn可用性组被拆成了两部分:理论部分和实战部分.而实战部分又被拆成了准备工作和AlwaysOn可用性组搭建. 三篇文章各自的链接: SQL Server ->> ...

  9. C#创建Window服务图解,安装、配置、以及C#操作Windows服务

    一.首先打开VS2013,创建Windows服务项目 二.创建完成后对"Service1.cs"重命名位"ServiceDemo":然后切换到代码视图,写个服务 ...

随机推荐

  1. 使用maven的profile切换项目各环境的参数

    Java后端开发经常需要面对管理多套环境,一般有三种环境:开发,测试,生产. 各个环境之间的参数各不相同,比如MySQL.Redis等不同环境的host不一样,若每个环境都手动替换环境很容易出错,Ma ...

  2. 挑战黑客极限:Pwn2Own 2015成史上“最难”黑客大赛

    Pwn2Own是全球最著名.奖金最丰厚的黑客大赛,由美国五角大楼入侵防护系统供应商TippingPoint赞助.近日Pwn2Own 2015公布全新的比赛规则,本届赛事难度超高.史无前例,包括VUPE ...

  3. WEB漏洞挖掘技术总结

    漏洞挖掘技术一直是网络攻击者最感兴趣的问题,漏洞挖掘的范围也在随着技术的提升而有所变化.在前期针对缓冲区溢出.格式化字符串.堆溢出.lib库溢出等技术都是针对ELF文件(Linux可执行文件)或者PE ...

  4. Hadoop 基本架构

    Hadoop 由两部分组成,分别是分布式文件系统和分布式计算框架 MapReduce. 其中分布式文件系统主要用于大规模数据的分布式存储,而 MapReduce 则构建在分布式文件系统之上,对存储在分 ...

  5. mysql5.0.x统计每秒增删改查替换数及系统每秒磁盘IO

    转载自:http://blog.chinaunix.net/uid-9370128-id-393082.html 1. mysql 5.0.x 统计每秒增,删,改,查,替换数  mysql 的show ...

  6. Open DJ备份与恢复方案

    最近接手了一个Cognos项目,第三方用户认证采用的是和Open DJ集成.本人之前很多采用的是cjap ,当然这和cjap相比起来简单的多了,最起码你不必具有Java的基础知识就可以完全驾驭了! 一 ...

  7. [Grunt] Concatenating Your Javascript with grunt-contrib-concat

    Combine serval javascript files together. For angular project, make sure you add angular.min.js firs ...

  8. [Grunt] Watch && grunt-contrib-watch

    Watch is an essential component of any Grunt build, and you will find yourself using it in almost ev ...

  9. (算法)二叉树的第m层第k个节点

    题目: 给定以下二叉树: struct node { node *left, *right; int value; }; 要求编写函数 node* foo(node *node, unsigned i ...

  10. T-SQL 之 触发器

    触发器可以做很多事情,但也会带来很多问题.正确的使用在于在适当的时候使用,而不要在不适当的时候使用它们. 触发器的一些常见用途如下: [1] 弹性参照完整性:实现很多DRI不能实现的操作(例如,跨数据 ...