弹来弹去跑马灯!

WP8.1 /silverlight 获取手机图片并且显示

参考:
http://www.cnblogs.com/tcjiaan/p/3930826.html 【WP 8.1开发】文件选取器的使用方法


MainPage.axml

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TestPhoneAppOpenFile.Resources;
using Windows.Storage.Pickers;
using Windows.Storage;
using Windows.Storage.FileProperties;
using System.Windows.Media.Imaging;
using Windows.Storage.Streams;
using System.IO;

namespace TestPhoneAppOpenFile
{
public partial class MainPage : PhoneApplicationPage
{
// 构造函数
public MainPage()
{
InitializeComponent();

// 用于本地化 ApplicationBar 的示例代码
//BuildLocalizedApplicationBar();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.NavigationMode == NavigationMode.Back && PickFiles.PickedFiles.Count > 0)
{

if (PickFiles.PickedFiles.Count > 0)
{
StorageFile file = PickFiles.PickedFiles[0];
tb.Text = file.Path;
getPic();
}

}
}

async void getPic() {

try {
StorageFile file = PickFiles.PickedFiles[0];

var itemThumbnail = await file.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.PicturesView, 550);
Stream stream = await file.OpenStreamForReadAsync();

BitmapImage img = new BitmapImage();

// img.SetSource(itemThumbnail.AsStream());
img.SetSource(stream);

PIC.Source = img;
MessageBox.Show(""+img.PixelWidth+";"+ img.PixelHeight);
// IBuffer buff = RandomAccessStreamToBuffer(samllThumb);//将IRandomAccessStream
}
catch (Exception ex){
MessageBox.Show(ex.Message );
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{

openFile();

}

async void openFile() {

FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;

openPicker.FileTypeFilter.Add(".jpg");

openPicker.FileTypeFilter.Add(".jpeg");

openPicker.FileTypeFilter.Add(".png");

openPicker.PickSingleFileAndContinue();    //只能选择一个文件

}

// 用于生成本地化 ApplicationBar 的示例代码
//private void BuildLocalizedApplicationBar()
//{
// // 将页面的 ApplicationBar 设置为 ApplicationBar 的新实例。
// ApplicationBar = new ApplicationBar();

// // 创建新按钮并将文本值设置为 AppResources 中的本地化字符串。
// ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
// appBarButton.Text = AppResources.AppBarButtonText;
// ApplicationBar.Buttons.Add(appBarButton);

// // 使用 AppResources 中的本地化字符串创建新菜单项。
// ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
// ApplicationBar.MenuItems.Add(appBarMenuItem);
//}
}
}

建个类来保存选择的图文件信息:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;

namespace TestPhoneAppOpenFile
{
public class PickFiles
{

private static List files = null;
static PickFiles()
{
if (files == null)
{
files = new List();
}
}
public static List PickedFiles
{
get { return files; }
}

}
}


App.xaml.cs

private void Application_ContractActivated(object sender, Windows.ApplicationModel.Activation.IActivatedEventArgs e)
{
if (e.Kind == Windows.ApplicationModel.Activation.ActivationKind.PickFileContinuation)
{
Windows.ApplicationModel.Activation.FileOpenPickerContinuationEventArgs ca = e as Windows.ApplicationModel.Activation.FileOpenPickerContinuationEventArgs;
PickFiles.PickedFiles.Clear();
foreach (var f in ca.Files)
{
PickFiles.PickedFiles.Add(f);
}
}
}

posted @ 2015-09-16 04:05  wgscd  阅读(139)  评论(0)    收藏  举报