使用MediaStreamSource捕获图像

时间:2014-03-16 20:12:22

标签: windows-phone-8 camera windows-phone media-library

我使用MediaStreamSource来使用相机......一切正常,除非我尝试拍摄图像!

我认为问题是对象MediaStreamSource

public class CameraStreamSourceModel : MediaStreamSource
{
    private MemoryStream _cameraStream = null; // here I've the stream from camera

    ...

    public async void CapturePhoto()
    {
        // Save the image as a jpeg to the camera roll 
        MediaLibrary library = new MediaLibrary();

        string filename = AppResources.ApplicationTitle + "_" + DateTime.Now.ToString("G");
        Picture pic = library.SavePicture(filename, _cameraStream);  //Here I've the exception
    }
}

例外是

  

System.InvalidOperationException:发生意外错误。

我已启用ID_CAP_MEDIALIB_PHOTO

我确信保存图像的代码有效,因为我可以在媒体库中保存静态流,但不能从相机中保存!

任何人都可以帮助我吗?谢谢

1 个答案:

答案 0 :(得分:0)

你可以简单地使用camera_capture_task

CameraCaptureTask cameraCaptureTask;


public Transaction()
        {
            InitializeComponent();
            cameraCaptureTask = new CameraCaptureTask();
            cameraCaptureTask.Completed += new EventHandler<PhotoResult>(cameraCaptureTask_Completed);
        }

void cameraCaptureTask_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {


            //Code to display the photo on the page in an image control named myImage.
            System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
            bmp.SetSource(e.ChosenPhoto);
                pic.Source = bmp;
            pic_name = "" + DateTime.Now.Month + "" + DateTime.Now.Hour + "" + DateTime.Now.Minute + "" + DateTime.Now.Second+".jpeg";
            SaveToIsolatedStorage(e.ChosenPhoto, "" + pic_name);
        }
    }