Welcome

首页 / 软件开发 / .NET编程技术 / Windows 8 Store Apps学习(29) 图片处理

Windows 8 Store Apps学习(29) 图片处理2013-12-06 cnblogs webabcd介绍

重新想象 Windows 8 Store Apps 之 图片处理

显示图片

图片的 9 切片

WriteableBitmap

获取和修改图片属性

对图片文件做“缩放/旋转/编码”操作,并保存 操作后的结果

示例

1、演示最基础的图片显示

Image/Display.xaml

<Pagex:Class="XamlDemo.Image.Display"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="using:XamlDemo.Image"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d"><Grid Background="Transparent"><StackPanel Margin="120 0 0 0" Orientation="Horizontal" VerticalAlignment="Top"><Border BorderBrush="Red" BorderThickness="1" Width="200" Height="100" Margin="20 0 0 0"><Image Source="/Assets/Logo.png" Stretch="Uniform" Width="200" Height="100" /></Border><Border BorderBrush="Red" BorderThickness="1" Width="200" Height="100" Margin="20 0 0 0"><Image Source="ms-appx:///Assets/Logo.png" Stretch="Uniform" Width="200" Height="100" /></Border><Border BorderBrush="Red" BorderThickness="1" Width="200" Height="100" Margin="20 0 0 0"><Image x:Name="img" Stretch="Uniform" Width="200" Height="100" /></Border><Border BorderBrush="Red" BorderThickness="1" Width="200" Height="100" Margin="20 0 0 0"><Image x:Name="img2" Stretch="Uniform" Width="200" Height="100" /></Border></StackPanel></Grid></Page>
Image/Display.xaml.cs

/* * 演示最基础的图片显示 ** 注: * 1、引用 package 中的图片用:ms-appx:/// * 2、引用 ApplicationData 中的图片: *a) LocalFolder 对应 ms-appdata:///local/ *b) RoamingFolder 对应 ms-appdata:///roaming/ *c) TemporaryFolder 对应 ms-appdata:///temp/ */using System;using Windows.Storage.Streams;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Media.Imaging;using Windows.UI.Xaml.Navigation;namespace XamlDemo.Image{public sealed partial class Display : Page{public Display(){this.InitializeComponent();}protected async override void OnNavigatedTo(NavigationEventArgs e){// code-behind 指定图片源img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Logo.png", UriKind.Absolute));// code-behind 指定图片源RandomAccessStreamReference imageStreamRef = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/Logo.png", UriKind.Absolute));IRandomAccessStream imageStream = await imageStreamRef.OpenReadAsync();BitmapImage bitmapImage = new BitmapImage();bitmapImage.SetSource(imageStream);img2.Source = bitmapImage;}}}
2、演示图片的 NineGrid

Image/NineGrid.xaml

<Pagex:Class="XamlDemo.Image.NineGrid"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="using:XamlDemo.Image"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d"><Grid Background="Transparent"><StackPanel Margin="120 0 0 0" Orientation="Horizontal" VerticalAlignment="Top"><!--Image - 图片控件NineGrid - 指定9网格(相当于flash中的9切片)中的4条线,Thickness 类型Left - 左边的线相对于图片最左端的距离Top - 上边的线相对于图片最顶端的距离Right - 右边的线相对于图片最右端的距离Bottom - 下边的线相对于图片最底端的距离以下示例图片的原始大小为 16 * 16--><Image Source="/Assets/NineGrid/Demo.png" Width="200" Height="200" /><!--通过指定9切片,防止边框被放大或缩小--><Image Source="/Assets/NineGrid/Demo.png" Width="200" Height="200" NineGrid="1 1 1 1" Margin="20 0 0 0" /></StackPanel></Grid></Page>