起動すると選んだ(撮った)写真をタイルにするアプリ

#wpdev_jp

タイトルの通り、起動するといきなりPictureハブが起動するので写真を撮るか写真を選ぶとそれがタイルになるアプリです。

imageカギは4つ

  1. 撮影したらいったん分離ストレージに保存する
  2. 分離ストレージに保存した画像をタイルで使うには、
    画像が /Shared/ShellContent にいないといけない
  3. ファイル名はユニークにする
    (かぶると名前が変わって読めない tile.jpg → tile(1).jpg)
  4. タイルを複数作るためにはURLをユニークにする

 

using System;
using System.Collections.Generic;
:
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Shell;

namespace PhoneApp49
{
public partial class MainPage : PhoneApplicationPage
{
// コンストラクター
        public MainPage()
{
InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            PhotoChooserTask task = new PhotoChooserTask {
                ShowCamera = true, PixelHeight = 173, PixelWidth = 173 };
            task.Completed += (s, ex) =>
            {
                if (ex.TaskResult == TaskResult.OK)
                {
                    using (IsolatedStorageFile IsoStore =
                        IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        String uq = DateTime.Now.ToString("yyyyMMddhhmmss");
                        String filename = String.Format("/Shared/ShellContent/{0}.jpg", uq);
                        IsolatedStorageFileStream IsoStrm = IsoStore.CreateFile(filename);
                        BitmapImage bmp = new BitmapImage();
                        bmp.SetSource(ex.ChosenPhoto);
                        WriteableBitmap wb = new WriteableBitmap(bmp);
                        wb.SaveJpeg(IsoStrm, wb.PixelWidth, wb.PixelHeight, 0, 100);
                        IsoStrm.Close();
                        StandardTileData tile = new StandardTileData {
                            BackgroundImage = new Uri("isostore:" + filename, UriKind.Absolute) };
                        ShellTile.Create(new Uri("/MainPage.xaml?="+uq, UriKind.Relative), tile);
                    }
                }
            };
            task.Show();
        }

    }
}