使用silverlight将捕获图像保存到文件夹/数据库? [英] save a capture image to a folder/database using silverlight?
本文介绍了使用silverlight将捕获图像保存到文件夹/数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好朋友,
这里我将在我的项目中通过网络摄像头将图像保存到文件夹/数据库中,也可以用于显示。请帮我解决这个问题。
这里我尝试了这个,但无法将图像保存到文件夹< br $>
代码:
hello friends ,
here iam going to do in my project saving a image through webcam into a folder/database viceversa retriving also for displaying . please help me out how to solve this problem.
here i tried this one but not able to save an image to a folder
code:
namespace SilverlightDemo
{
public partial class MainPage : UserControl
{
CaptureSource _captureSource;
ObservableCollection<writeablebitmap> _images = new ObservableCollection<writeablebitmap>();
private WriteableBitmap selectedSnapshot;
public WriteableBitmap SelectedSnapshot
{
get { return selectedSnapshot; }
set { selectedSnapshot = value; }
}
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
// Get list of the VIDEO Sources and bind
VideoSources.ItemsSource = CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices();
// Get list of the AUDIO Sources and bind
AudioSources.ItemsSource = CaptureDeviceConfiguration.GetAvailableAudioCaptureDevices();
// Select the default devices
if (VideoSources.Items.Count > 0)
VideoSources.SelectedIndex = 0;
if (AudioSources.Items.Count > 0)
AudioSources.SelectedIndex = 0;
// Creating CaptureSource
_captureSource = new CaptureSource();
// Handle CaptureImageAsync Completed event handler
_captureSource.CaptureImageCompleted += (s, ev) =>
{
ProcessImage(ev.Result);
};
// Bind snapshots
Snapshots.ItemsSource = _images;
// Disable the capture button, it'll be enabled when capture source is ready
CaptureWebcam.IsEnabled = false;
SaveImage.IsEnabled = false;
}
private void StartStopWebcam_Click(object sender, RoutedEventArgs e)
{
StartStopWebcamCapture();
}
private void CaptureWebcam_Click(object sender, RoutedEventArgs e)
{
// Verify the device is started
if (_captureSource.VideoCaptureDevice != null && _captureSource.State == CaptureState.Started)
{
// Capture the current frame
_captureSource.CaptureImageAsync();
}
}
private void StartStopWebcamCapture()
{
try
{
if (_captureSource != null)
{
//Start Capturing
if (_captureSource.State != CaptureState.Started)
{
// Set the devices
_captureSource.VideoCaptureDevice = (VideoCaptureDevice)VideoSources.SelectedItem;
_captureSource.AudioCaptureDevice = (AudioCaptureDevice)AudioSources.SelectedItem;
// Video Brush
VideoBrush vBrush = new VideoBrush();
vBrush.SetSource(_captureSource);
Webcam.Fill = vBrush; // Paint the video brush on the rectangle
// Request user permission
if (CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess())
{
if (_captureSource.VideoCaptureDevice != null)
{
_captureSource.Start();
StartStopWebcam.Content = "Stop Camera";
CaptureWebcam.IsEnabled = true;
}
}
}
// Stop Capturing
else
{
if (_captureSource.VideoCaptureDevice != null)
{
_captureSource.Stop();
StartStopWebcam.Content = "Start Camera";
CaptureWebcam.IsEnabled = false;
SaveImage.IsEnabled = false;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error using webcam", MessageBoxButton.OK);
}
}
private void ProcessImage(WriteableBitmap bitmap)
{
_images.Add(bitmap);
SelectedSnapshot = bitmap;
SaveImage.IsEnabled = true;
}
private void SaveImage_Click(object sender, RoutedEventArgs e)
{
// Gets the current captured raw bitmap
var rawImage = SelectedSnapshot;
// If image is selected in the Snapshots ListBox then set it as a save target
if (Snapshots.SelectedItem != null)
rawImage = (WriteableBitmap)Snapshots.SelectedItem;
if (rawImage != null)
{
// Init the Save File Dialog
//SaveFileDialog saveDialog = new SaveFileDialog();
//saveDialog.Filter = "Image Files (*.jpg, *.jpeg)|*.jpg;*.jpeg|All Files (*.*)|*.*";
//saveDialog.DefaultExt = ".jpg";
//saveDialog.FilterIndex = 1;
//// Show save dialog to the user
//if ((bool)saveDialog.ShowDialog())
//{
// using (Stream stream = saveDialog.OpenFile())
// {
// // Convert raw captured bitmap to the image that Image Tools understand with the extension method
// var image = rawImage.ToImage();
// // Declare jpeg encoder
// var encoder = new JpegEncoder();
// // Set the image quality
// //encoder.Quality = 90;
// // Finally encode raw bitmap and save it as a jpg image
// encoder.Encode(image, stream);
// // Close the stream
// stream.Close();
}
}
}
}
}
请帮帮我
而不是通过savedilog,应该保存在文件夹/ db中。
感谢你。
please help me out
instead of saving through a savedilog, should save in folder/db.
thanking you.
推荐答案
如果你正在使用任何服务即RIA,WCF服务然后你必须在那里写你的文件编写代码。以下是RIA服务中文件编写的示例。
只需从快照中获取二进制文件,并使用二进制文件名和要保存的文件名调用此函数编写。
If you are using any Service i.e. RIA, WCF service then you have to write your file writing code there. here is the example of file writing in RIA service.
Just get binaries from your snapshot and call this function written in service with parameters of binary and file name you want to save.
public bool Upload(string p_szfileName, Byte[] p_bImage)
{
FileStream fileStream = null;
BinaryWriter writer = null;
string filePath = string.Empty;
try
{
string szDirectory = WebConfigurationManager.AppSettings["VirtualDirectory"].ToString();
filePath = HttpContext.Current.Server.MapPath(szDirectory + "Your Path--") + p_szfileName;
if (p_bImage != null)
{
fileStream = File.Open(filePath, FileMode.Create);
writer = new BinaryWriter(fileStream);
writer.Write(p_bImage);
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (fileStream != null)
fileStream.Close();
if (writer != null)
writer.Close();
}
}
这篇关于使用silverlight将捕获图像保存到文件夹/数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文