【问题标题】:How to access the camera from my Windows Phone 8 app (XAML and C#) and save the taken picture in a determined folder?如何从我的 Windows Phone 8 应用程序(XAML 和 C#)访问相机并将拍摄的照片保存在确定的文件夹中?
【发布时间】:2026-01-18 08:00:01
【问题描述】:

我希望我现在正在构建的 Windows Phone 8 应用程序能够在按下屏幕上的具体按钮时访问相机以拍照,然后将已拍摄的图像保存到确定的文件夹(文件夹由我创建到 Windows Phone 项目中,而不是 Windows Phone 默认图片库)。

您能帮我打开相机,拍照并将其保存到我创建的文件夹中吗?我正在使用 XAML 和 C#。

非常感谢!!!

【问题讨论】:

    标签: c# windows-phone-8 camera save directory


    【解决方案1】:

    如果要在应用程序中的按钮上处理捕获,我会推荐 PhotoCamera 类

     PhotoCamera myCamera = new Microsoft.Devices.PhotoCamera(CameraType.Primary);
     //viewfinderBrush is a videobrush object declared in xaml
     viewfinderBrush.SetSource(myCamera);
     myCamera.Initialized += myCamera_Initialized;
     myCamera.CaptureCompleted += new EventHandler<CameraOperationCompletedEventArgs>(camera_CaptureCompleted);
     myCamera.CaptureImageAvailable += new EventHandler<Microsoft.Devices.ContentReadyEventArgs>(camera_CaptureImageAvailable);
    

    //事件

       void myCamera_Initialized(object sender, CameraOperationCompletedEventArgs e)
        {
            try
            {
                if (e.Succeeded)
                {
    
                }
            }
            catch
            {
                MessageBox.Show("Problem occured in camera initialization.");
            }
        }
    
     void camera_CaptureCompleted(object sender, CameraOperationCompletedEventArgs e)
            {
                try
                {
    
                }
                catch
                {
                    MessageBox.Show("Captured image is not available, please try again.");
                }
            }
    
    void camera_CaptureImageAvailable(object sender, Microsoft.Devices.ContentReadyEventArgs e)
            {
                try
                {
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Captured image is not available, please try again.   " + ex.Message);
                }
    
            }
    

    还有另一种选择,称为 CameraCaptureTask

    CameraCaptureTask cameraCaptureTask;
    cameraCaptureTask = new CameraCaptureTask();
    cameraCaptureTask.Completed += new EventHandler<PhotoResult>(cameraCaptureTask_Completed);
    cameraCaptureTask.Show();
    
    void cameraCaptureTask_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            MessageBox.Show(e.ChosenPhoto.Length.ToString());
    
            //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);
            //myImage.Source = bmp;
        }
    }
    

    检查 this 的 PhotoCamera 类

    还有 this 用于 CameraCaptureTask

    【讨论】:

      【解决方案2】:

      这里有一个简单的代码演示,显示您将相机 API 用于 Windows Phone8 应用程序。

       private void MainPage_Loaded(object sender, RoutedEventArgs e)
       {
      
       if ((PhotoCamera.IsCameraTypeSupported(CameraType.Primary) == true) ||
           (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing) == true))  {
           // Initialize the default camera.
           _photoCamera = new Microsoft.Devices.PhotoCamera();
      
           //Event is fired when the PhotoCamera object has been initialized
           _photoCamera.Initialized += new EventHandler<Microsoft.Devices.CameraOperationCompletedEventArgs>(OnPhotoCameraInitialized);
      
            //Set the VideoBrush source to the camera
            viewfinderBrush.SetSource(_photoCamera);
        }
        else {
            // The camera is not supported on the device.
            this.Dispatcher.BeginInvoke(delegate()  {
            // Write message.
                txtDebug.Text = "A Camera is not available on this device.";
            });
      
        }
      
      }
      
      private void OnPhotoCameraInitialized(object sender, CameraOperationCompletedEventArgs e) {
          int width = Convert.ToInt32(_photoCamera.PreviewResolution.Width);
          int height = Convert.ToInt32(_photoCamera.PreviewResolution.Height);
      }
      

      不要忘记在 WMAppManifent.xml 文件中添加这一行。

      <Capability Name="ID_CAP_ISV_CAMERA"/>
      

      你可以在这里阅读,

      Using Cameras in Your Windows Phone Application

      【讨论】:

        最近更新 更多