コード例 #1
0
ファイル: MainViewModel.cs プロジェクト: tpetrina/ImagingApps
        private async void ChooseExecute()
        {
            CanChoose = false;
            try
            {
                var chooser = new PhotoChooserTask {
                    ShowCamera = true
                };
                var first = await chooser.ShowAsync();

                if (first.ChosenPhoto == null)
                {
                    var prompt = new ToastPrompt {
                        Message = "Gotta choose something man..."
                    };
                    prompt.Show();
                    return;
                }

                var second = await chooser.ShowAsync();

                if (second.ChosenPhoto == null)
                {
                    var prompt = new ToastPrompt {
                        Message = "Also choose second one..."
                    };
                    prompt.Show();
                    return;
                }

                var bi1 = new BitmapImage();
                bi1.SetSource(first.ChosenPhoto);

                var bi2 = new BitmapImage();
                bi2.SetSource(second.ChosenPhoto);

                WriteableBitmap wb1;
                WriteableBitmap wb2;
                if (bi2.PixelHeight != bi1.PixelHeight || bi2.PixelWidth != bi1.PixelWidth)
                {
                    // sorry for bad croping
                    var toast = new ToastPrompt {
                        Message = "images are not of the same size, lame cropping incoming :/"
                    };
                    toast.Show();

                    EditingSession session1 = null, session2 = null;
                    using (var stream1 = bi1.ToStream())
                        using (var stream2 = bi2.ToStream())
                            try
                            {
                                var task1    = EditingSessionFactory.CreateEditingSessionAsync(stream1);
                                var task2    = EditingSessionFactory.CreateEditingSessionAsync(stream2);
                                var sessions = await Task.WhenAll(task1, task2);

                                session1 = task1.Result;
                                session2 = task2.Result;

                                var cropRect = new Windows.Foundation.Rect(0, 0,
                                                                           Math.Min(bi1.PixelWidth, bi2.PixelWidth),
                                                                           Math.Min(bi1.PixelHeight, bi2.PixelHeight));

                                sessions[0].AddFilter(FilterFactory.CreateCropFilter(cropRect));
                                sessions[1].AddFilter(FilterFactory.CreateCropFilter(cropRect));
                                wb1 = new WriteableBitmap((int)cropRect.Width, (int)cropRect.Height);
                                wb2 = new WriteableBitmap((int)cropRect.Width, (int)cropRect.Height);

                                await Task.WhenAll(
                                    sessions[0].RenderToWriteableBitmapAsync(wb1),
                                    sessions[1].RenderToWriteableBitmapAsync(wb2)
                                    );
                            }
                            finally
                            {
                                Helpers.SafeDispose(ref session1);
                                Helpers.SafeDispose(ref session2);
                            }
                }
                else
                {
                    wb1 = new WriteableBitmap(bi1);
                    wb2 = new WriteableBitmap(bi2);
                }

                // save images
                using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                    using (IsolatedStorageFileStream fileStream1 = isoStore.CreateFile(FirstFileName),
                           fileStream2 = isoStore.CreateFile(SecondFileName))
                    {
                        wb1.SaveJpeg(fileStream1, wb1.PixelWidth, wb1.PixelHeight, 0, 100);
                        wb2.SaveJpeg(fileStream2, wb2.PixelWidth, wb2.PixelHeight, 0, 100);
                    }

                FirstImage  = wb1;
                SecondImage = wb2;

                ApplyExecute();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {
                CanChoose = true;
            }
        }