|
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
namespace KinectReception.Controls
{
/// <summary>
/// Interaction logic for CameraView.xaml
/// </summary>
public partial class CameraView : UserControl
{
private DispatcherTimer _previewTimer;
public bool IsWorking
{
get { return _previewTimer.IsEnabled; }
}
public CameraView()
{
this.InitializeComponent();
Initialize();
}
public void Initialize()
{
_previewTimer = new DispatcherTimer();
_previewTimer.Interval = Properties.Settings.Default.PreviewTimer;
_previewTimer.Tick -= PreviewTimerTick;
_previewTimer.Tick += new EventHandler(PreviewTimerTick);
}
void PreviewTimerTick(object sender, EventArgs e)
{
_previewTimer.Stop();
Reset();
this.Visibility = Visibility.Hidden;
}
private void Reset()
{
txtDisplayText.Text = Properties.Resources.PreviewTitle;
txtScore.Text = string.Empty;
PreviewPicture.Source = null;
txtScore.Visibility = Visibility.Visible;
}
public void TakePicture(ImageSource imgSource)
{
txtScore.Visibility = Visibility.Hidden;
SavePreview(imgSource);
}
public void TakePicture(ImageSource imgSource, int score)
{
txtScore.Text = string.Format(Properties.Resources.PreviewScoreText, score);
SavePreview(imgSource);
}
public void TakePicture(ImageSource imgSource, string displayText, int score)
{
txtDisplayText.Text = displayText;
txtScore.Text = string.Format(Properties.Resources.PreviewScoreText, score);
SavePreview(imgSource);
}
private void SavePreview(ImageSource imgSource)
{
if (string.IsNullOrEmpty(Properties.Settings.Default.PicturesLibrary) || !Directory.Exists(Properties.Settings.Default.PicturesLibrary) || imgSource == null)
return;
else
{
var newPictureName = System.IO.Path.Combine(Properties.Settings.Default.PicturesLibrary,
string.Format("{0}.png", System.IO.Path.GetRandomFileName()));
const int width = 640;
const int height = 480;
const int stride = width;
byte[] pixels = new byte[height * stride];
// Define the image palette
var myPalette = BitmapPalettes.Halftone256;
var image = (BitmapSource)imgSource;
PreviewPicture.Source = image; // new DrawingImage(VisualTreeHelper.GetDrawing(KinectImage));
this.Visibility = Visibility.Visible;
_previewTimer.Start();
using (var stream = new FileStream(newPictureName, FileMode.Create))
{
var encoder = new PngBitmapEncoder();
encoder.Interlace = PngInterlaceOption.On;
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(stream);
}
}
}
//void TakePicture()
//{
// //if (string.IsNullOrEmpty(Properties.Settings.Default.PicturesLibrary) || !Directory.Exists(Properties.Settings.Default.PicturesLibrary))
// // return;
// //else
// //{
// // var newPictureName = System.IO.Path.Combine(Properties.Settings.Default.PicturesLibrary,string.Format("{0}.png",System.IO.Path.GetRandomFileName()));
// // int width = 640;
// // int height = 480;
// // int stride = width;
// // byte[] pixels = new byte[height * stride];
// // // Define the image palette
// // BitmapPalette myPalette = BitmapPalettes.Halftone256;
// // BitmapSource image = (BitmapSource)KinectImage.Source;
// // PreviewPicture.Source = image;// new DrawingImage(VisualTreeHelper.GetDrawing(KinectImage));
// // PreviewPanel.Visibility = Visibility.Visible;
// // _previewTimer.Start();
// // FileStream stream = new FileStream(newPictureName, FileMode.Create);
// // PngBitmapEncoder encoder = new PngBitmapEncoder();
// // encoder.Interlace = PngInterlaceOption.On;
// // encoder.Frames.Add(BitmapFrame.Create(image));
// // encoder.Save(stream);
// //var stream = new FileStream(newPictureName, FileMode.Create);
// //var vis = new DrawingVisual();
// //var cont = vis.RenderOpen();
// //cont.DrawImage(PreviewPicture.Source, new Rect(new Size(640d, 480d)));
// //cont.Close();
// //var rtb = new RenderTargetBitmap((int)PreviewPicture.Width,(int)PreviewPicture.Height, 96d, 96d, PixelFormats.Default);
// //rtb.Render(vis);
// //var encoder = new PngBitmapEncoder();
// //encoder.Frames.Add(BitmapFrame.Create(rtb));
// //encoder.Save(stream);
// //stream.Close();
// // }
//}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Shai Raiten is VS ALM MVP, currently working for Sela Group as a ALM senior consultant and trainer specializes in Microsoft technologies especially Team System and .NET technology. He is currently consulting in various enterprises in Israel, planning and analysis Load and performance problems using Team System, building Team System customizations and adjusts ALM processes for enterprises. Shai is known as one of the top Team System experts in Israel. He conducts lectures and workshops for developers\QA and enterprises who want to specialize in Team System.
My Blog: http://blogs.microsoft.co.il/blogs/shair/