Exemplo n.º 1
0
        /// <summary>
        /// Internals the on photo chosen.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="photoResult">The photo result.</param>
        private void InternalOnPhotoChosen(object sender, PhotoResult photoResult)
        {
            var tcs = Interlocked.Exchange(ref _completionSource, null);

            if (photoResult.TaskResult == TaskResult.Cancel)
            {
                tcs.SetCanceled();
                return;
            }

            var path = string.Empty;

            var           pos         = photoResult.ChosenPhoto.Position;
            var           options     = tcs.Task.AsyncState as CameraMediaStorageOptions;
            var           streamImage = photoResult.ChosenPhoto;
            var           saveImage   = true;
            Action <bool> dispose     = null;

            if (options != null)
            {
                ResizeImageStream(
                    options.MaxPixelDimension,
                    options.PercentQuality,
                    streamImage,
                    stream => SafeAsyncCall(stream, o => streamImage = o));
                saveImage = options.SaveMediaOnCapture;
            }

            if (saveImage)
            {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    path = options.GetUniqueMediaFileWithPath((options == null) ? "temp" : options.Directory, p => store.FileExists(p));

                    var dir = Path.GetDirectoryName(path);
                    if (!String.IsNullOrWhiteSpace(dir))
                    {
                        store.CreateDirectory(dir);
                    }

                    using (var fs = store.CreateFile(path))
                    {
                        var buffer = new byte[20480];
                        int len;
                        //while ((len = photoResult.ChosenPhoto.Read(buffer, 0, buffer.Length)) > 0)
                        while ((len = streamImage.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            fs.Write(buffer, 0, len);
                        }

                        fs.Flush(true);
                    }
                }

                if (options == null)
                {
                    dispose = d =>
                    {
                        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                        {
                            store.DeleteFile(path);
                        }
                    };
                }
            }

            switch (photoResult.TaskResult)
            {
            case TaskResult.OK:
                photoResult.ChosenPhoto.Position = pos;
                var mf = new MediaFile(path, () => streamImage, dispose);

                if (OnMediaSelected != null)
                {
                    OnMediaSelected(this, new MediaPickerArgs(mf));
                }

                tcs.SetResult(mf);
                break;

            case TaskResult.None:
                photoResult.ChosenPhoto.Dispose();
                if (photoResult.Error != null)
                {
                    if (OnError != null)
                    {
                        OnError(this, new MediaPickerErrorArgs(photoResult.Error));
                    }

                    tcs.SetException(photoResult.Error);
                }

                break;
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MediaPickerArgs" /> class.
 /// </summary>
 /// <param name="mf">The mf.</param>
 public MediaPickerArgs(MediaFile mf)
 {
     MediaFile = mf;
 }