首先声明,本文章是转载:http://www.cnblogs.com/alexis/archive/2011/05/26/2052319.html
wp7 sdk中文版出来一段时间了,由于最近比较忙,也没有去尝试下,经过一上午的整体配置,计算机终于可以顺利的跑 wp7项目了,呵呵,根据博客园这位老兄的日志,写了一个小程序,权作学习的开端。
需求很简单,打开一幅图片,自定义背景图片,为了将程序下次打开的时候,能够不丢失图片,我们将其保存到手机内存中去,不罗嗦,直接晒demo:
汗...博客园不让上传图片了...这么回事...好吧.先晒前台代码:
<phone:PhoneApplicationPage
x:Class="PhoneApp1SaveImg.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot 是包含所有页面内容的根网格-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel 包含应用程序的名称和页标题-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="我的第一个测试" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="设置背景图片" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - 在此处放置其他内容-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="打开" Height="72" HorizontalAlignment="Left" Margin="134,58,0,0" Name="openImg" VerticalAlignment="Top" Width="160" Click="openImg_Click" />
<Button Content="加载" Height="72" HorizontalAlignment="Left" Margin="134,170,0,0" Name="loadImg" VerticalAlignment="Top" Width="160" Click="loadImg_Click" />
</Grid>
</Grid>
</phone:PhoneApplicationPage>
后台代码:
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 Microsoft.Phone.Tasks;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;
using Microsoft.Phone;
namespace PhoneApp1SaveImg
{
public partial class MainPage : PhoneApplicationPage
{
// 构造函数
public MainPage()
{
InitializeComponent();
}
byte[] imageByte = null;
//加载本地图片
private void openImg_Click(object sender, RoutedEventArgs e)
{
//图片选择器
PhotoChooserTask task = new PhotoChooserTask();
task.Completed += new EventHandler<PhotoResult>(task_Completed);
task.Show();
}
void task_Completed(object sender, PhotoResult e)
{
imageByte = new byte[e.ChosenPhoto.Length];
e.ChosenPhoto.Read(imageByte, 0, imageByte.Length);
//保存图片
SaveToLocalStorage(imageByte, "myImg", "Images");
}
//将图片保存到本地
public static void SaveToLocalStorage(byte[] _imageBytes, string imageFileName, string imageFolder)
{
if (_imageBytes == null)
{
return;
}
var isFile = IsolatedStorageFile.GetUserStoreForApplication();//获取独立存储空间
if (!isFile.DirectoryExists(imageFolder))
{
isFile.CreateDirectory(imageFolder);
}
string filePath = System.IO.Path.Combine(imageFolder, imageFileName);
using(var sream=isFile.CreateFile(filePath))
{
sream.Write(_imageBytes,0,_imageBytes.Length); //写入文件
}
}
//读取图片
public static WriteableBitmap LoadImageFromLocalStorge(string imageFileName, string imageFolder)
{
var isFile = IsolatedStorageFile.GetUserStoreForApplication();
if (!isFile.DirectoryExists(imageFolder))
{
isFile.CreateDirectory(imageFolder);
}
string filePath = System.IO.Path.Combine(imageFolder, imageFileName);
using (var imageSream = isFile.OpenFile(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
//将图片转换成wirteableBitmap
WriteableBitmap imageSource = PictureDecoder.DecodeJpeg(imageSream);
return imageSource;
}
}
//读取图片
private void loadImg_Click(object sender, RoutedEventArgs e)
{
ImageBrush myBrush=new ImageBrush();
ContentPanel.Background = new ImageBrush()
{
ImageSource=LoadImageFromLocalStorge("myImg", "Images")
};
}
}
}
算了..睡觉了...不让发图片...闹心