您的位置:首页 > 其它

Windows Phone在隔离存储里存取图片文件

2011-11-04 23:13 351 查看
作为Windows Phone的初学者,第一个练手小应用是一个类似备忘录的软件,功能不是太多,涉及到即时任务,灵感,图文的一些记录。对于即时拍照然后附上相应描述再保存在独立存储里面,刚开始不太懂,搞了好几天(毕竟是新手),总算是搞定了,在此奉上关于照片存取的小小收获,给予像我一样的新手一点小小的帮助吧,也许对有些人有用而对有些人不值一提吧。

一共两个页面,第一个为MainPage.xaml,代码如下:

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Image Name="PhotoImage" Height="342" HorizontalAlignment="Left" Margin="9,6,0,0"  Stretch="Fill" VerticalAlignment="Top" Width="441"/>
<Button Name="SaveButton" Click="SaveButton_Click" Content="保存" Height="72" HorizontalAlignment="Left" Margin="7,482,0,0"  VerticalAlignment="Top" Width="441">
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FFEB1818" Offset="1"/>
</LinearGradientBrush>
</Button.Background>
</Button>
<TextBlock Height="30" HorizontalAlignment="Left" Margin="27,442,0,0" Name="textBlock1" Text="输入照片名:" FontSize="25" VerticalAlignment="Top"/>
<TextBox Name="PhotoName" Height="72" HorizontalAlignment="Left" Margin="165,424,0,0"  Text="" VerticalAlignment="Top" Width="284">
<TextBox.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FFD01818" Offset="1"/>
</LinearGradientBrush>
</TextBox.Background>
</TextBox>
<Button Name="TakePhotoButton" Click="TakePhotoButton_Click" Content="拍照" Height="72" Margin="9,366,8,0" VerticalAlignment="Top">
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FFD61212" Offset="1"/>
</LinearGradientBrush>
</Button.Background>
</Button>
</Grid>
</Grid>

<!--Sample code showing usage of ApplicationBar-->
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Images/appbar.feature.search.rest.png" Text="查看" Click="ApplicationBarIconButton_Click"/>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>


后台代码如下:

Page1.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;

namespace 在隔离存储中存取照片
{
public partial class Page1 : PhoneApplicationPage
{
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
public Page1()
{
InitializeComponent();
}

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
List<PhotoFile> filelist = new List<PhotoFile>();
if (file.GetFileNames() != null)
{
foreach (string filename in file.GetFileNames())
{
PhotoFile photo = new PhotoFile();
BitmapImage bmp = new BitmapImage();
using (IsolatedStorageFileStream filestream = file.OpenFile(filename, FileMode.Open, FileAccess.ReadWrite))
{
bmp.SetSource(filestream);
filestream.Flush();
filestream.Close();
photo.Image = bmp;
photo.ImageName = " 照片名:"+filename;
}
filelist.Add(photo);
}
listBox1.ItemsSource = filelist;
}
}

private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/MainPage.xaml",UriKind.Relative));
}
}
}


运行效果如下:





希望对大家有所帮助,嘻嘻。祝大家WP开发更上一城楼,大家以后都去赚美刀!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: