2 maowade maowade 于 2016.03.24 10:21 提问

C#调用摄像头拍的照片是黑的

private void Page_Loaded(object sender, RoutedEventArgs e) {

        this.videoCaptureElement = new VideoCaptureElement();
        this.videoCaptureElement.BeginInit();
        this.videoCaptureElement.Width = 568;
        this.videoCaptureElement.Height = 364;

                    //videoCaptureElement position
                    Canvas.SetTop(videoCaptureElement, 200);
        Canvas.SetLeft(videoCaptureElement, 200);

        string[] inputNames = MultimediaUtil.VideoInputNames;
        this.videoCaptureElement.VideoCaptureSource = inputNames[0];
        this.videoCaptureElement.EndInit();
        this.videoCaptureElement.Play();
        this.CameraRenderLayer.Children.Add(this.videoCaptureElement);

        //button event

        TakePic.MouseLeftButtonDown += new MouseButtonEventHandler(takePhoto);

    }
    private void pagePrevious(object sender, RoutedEventArgs e) {

        this.NavigationService.Content = new BirthdayPage();
    }
    private void takePhoto(object sender, RoutedEventArgs e) {




        RenderTargetBitmap bmp = new RenderTargetBitmap((int)this.videoCaptureElement.ActualWidth, (int)this.videoCaptureElement.ActualHeight, 96, 96, PixelFormats.Pbgra32);
        bmp.Render(this.videoCaptureElement);
        BitmapEncoder encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bmp));
        string now = DateTime.Now.Year + "" + DateTime.Now.Month + "" + DateTime.Now.Day + "" + DateTime.Now.Hour + "" + DateTime.Now.Minute + "" + DateTime.Now.Second;
        string filename = "D:\\" + UserInfoSingleton.sharedInstance().UserName + "pic.jpg";
        FileStream fstream = new FileStream(filename, FileMode.Create);
        encoder.Save(fstream);
        fstream.Close();
        videoCaptureElement.Stop();

    }

如果我把Canvas.SetTop(videoCaptureElement, 200); Canvas.SetLeft(videoCaptureElement, 200);这两句注释掉,就能正常拍出照片,但是一旦调整了videoCaptureElement的位置拍出的照片就是黑色的

1个回答

CSDNXIAOD
CSDNXIAOD   2016.03.30 11:21

C#调用摄像头
[摘]C#摄像头调用
C#代码调用摄像头
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!