问题描述
- 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的位置拍出的照片就是黑色的
解决方案
时间: 2024-12-23 18:51:21