1

实际上,我使用相机加载 VideoBrush 以使用 Windows Phone 7.1 中的 PhotoCamera 选项拍摄对象的快照。

拍摄快照后,我将 e.ImageSteam 从 CaptureImageAvailable 事件设置为 ImageBrush 源。

拍摄快照后,调用 WindowsPhone 中的 CaptureImageAvailable 或 CaptureCompleted 事件需要时间。为什么会出现这种情况?

我需要写一些新的东西来解决这个问题吗?

4

1 回答 1

0

对于拍照和选择照片,您只需按照此操作,

MainPage.xaml:

<Button Content="Take Photo" Height="72" HorizontalAlignment="Left" Margin="0,10,0,0" Click="btntakephoto_Click" Name="btntakephoto" VerticalAlignment="Top" Width="456" />
            <Image Height="431" HorizontalAlignment="Left" Margin="10,92,0,0" Name="imgphoto" Stretch="Fill" VerticalAlignment="Top" Width="440" />
            <Button Content="Choose Photo" Height="72" HorizontalAlignment="Left" Margin="0,529,0,0" Click="btnchoosephoto_Click" Name="btnchoosephoto" VerticalAlignment="Top" Width="450" />

主页.cs

namespace TakePicture
{
    public partial class MainPage : PhoneApplicationPage
    {
        CameraCaptureTask camera = new CameraCaptureTask();
        PhotoChooserTask choosephoto = new PhotoChooserTask();

        public MainPage()
        {
            InitializeComponent();
            camera.Completed += new EventHandler<PhotoResult>(task_Completed);
            choosephoto.Completed += new EventHandler<PhotoResult>(task_Completed);
        }

        private void btntakephoto_Click(object sender, RoutedEventArgs e)
        {
            camera.Show();
        }

        private void btnchoosephoto_Click(object sender, RoutedEventArgs e)
        {
            choosephoto.Show();
        }

        private string SaveImageToLocalStorage(WriteableBitmap image, string imageFileName)
        {

            if (image == null)
            {
                throw new ArgumentNullException("imageBytes");
            }
            try
            {
                var isoFile = IsolatedStorageFile.GetUserStoreForApplication();

                if (!isoFile.DirectoryExists("MyPhotos"))
                {
                    isoFile.CreateDirectory("MyPhotos");
                }

                string filePath = System.IO.Path.Combine("/" + "MyPhotos" + "/", imageFileName);
                using (var stream = isoFile.CreateFile(filePath))
                {
                     image.SaveJpeg(stream, image.PixelWidth, image.PixelHeight, 0, 80);
                }

                return new Uri(filePath, UriKind.Relative).ToString();
            }
            catch (Exception)
            {
                //TODO: log or do something else
                throw;
            }
        }

        void task_Completed(object sender, PhotoResult e)
        {
            if (e.ChosenPhoto != null)
            {
                if (e.TaskResult == TaskResult.OK)
                {
                    try
                    {
                       string imagePathOrContent = string.Empty;
                       WriteableBitmap image = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
                       imgphoto.Source = image;
                       imagePathOrContent = this.SaveImageToLocalStorage(image, System.IO.Path.GetFileName(e.OriginalFileName));
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }
    }
}
于 2012-06-22T08:47:08.173 回答