Skip to content

Commit

Permalink
WebcamDevice usercontrol now support Width and Height as Auto in value
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaspalsinh Chauhan committed Jan 26, 2015
1 parent 293ef66 commit 1395166
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 27 deletions.
30 changes: 18 additions & 12 deletions TakeSnapsWithWebcamUsingWpfMvvm/MainWindow.xaml
Expand Up @@ -22,7 +22,7 @@
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
Expand All @@ -48,17 +48,23 @@
Grid.Row="1"
Text="Selected Device Preview:"
Margin="20,20,20,5" />
<video:WebcamDevice Grid.Row="2"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Grid.Column="0"
Grid.ColumnSpan="2"
Margin="20,0,20,20"
x:Name="CameraVideoDeviceControl"
VideoPreviewWidth="{Binding VideoPreviewWidth}"
VideoPreviewHeight="{Binding VideoPreviewHeight}"
VideoSourceId="{Binding SelectedVideoDevice, Converter={StaticResource MediaInformationConverter}}"
SnapshotBitmap="{Binding SnapshotBitmap, Mode=TwoWay}" />
<Grid Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Width="Auto" Height="Auto" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<video:WebcamDevice Grid.Row="0"
Grid.Column="0"
Margin="20,0,20,20"
x:Name="CameraVideoDeviceControl"
VideoPreviewWidth="Auto"
VideoPreviewHeight="Auto"
VideoSourceId="{Binding SelectedVideoDevice, Converter={StaticResource MediaInformationConverter}}"
SnapshotBitmap="{Binding SnapshotBitmap, Mode=TwoWay}" />
</Grid>

<Button Grid.Row="3"
Margin="20,20,20,5"
Grid.Column="0"
Expand Down
65 changes: 50 additions & 15 deletions TakeSnapsWithWebcamUsingWpfMvvm/Video/WebcamDevice.xaml.cs
Expand Up @@ -4,10 +4,12 @@

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

using AForge.Video.DirectShow;
Expand All @@ -27,12 +29,12 @@ public partial class WebcamDevice
/// <summary>
/// Dependency property for video display width.
/// </summary>
public static readonly DependencyProperty VideoPreviewWidthProperty = DependencyProperty.Register("VideoPreviewWidth", typeof(int), typeof(WebcamDevice), new PropertyMetadata(VideoPreviewWidthPropertyChangedCallback));
public static readonly DependencyProperty VideoPreviewWidthProperty = DependencyProperty.Register("VideoPreviewWidth", typeof(double), typeof(WebcamDevice), new PropertyMetadata(VideoPreviewWidthPropertyChangedCallback));

/// <summary>
/// Dependency property for video display height.
/// </summary>
public static readonly DependencyProperty VideoPreviewHeightProperty = DependencyProperty.Register("VideoPreviewHeight", typeof(int), typeof(WebcamDevice), new PropertyMetadata(VideoPreviewHeightPropertyChangedCallback));
public static readonly DependencyProperty VideoPreviewHeightProperty = DependencyProperty.Register("VideoPreviewHeight", typeof(double), typeof(WebcamDevice), new PropertyMetadata(VideoPreviewHeightPropertyChangedCallback));

/// <summary>
/// Dependency property for video device source Id.
Expand Down Expand Up @@ -96,11 +98,12 @@ public static IEnumerable<MediaInformation> GetVideoDevices
/// <remarks>
/// Important: Do not write any logic in dependency property stub.
/// </remarks>
public int VideoPreviewWidth
[TypeConverter(typeof(LengthConverter))]
public double VideoPreviewWidth
{
get
{
return (int)GetValue(VideoPreviewWidthProperty);
return (double)GetValue(VideoPreviewWidthProperty);
}

set
Expand All @@ -115,11 +118,12 @@ public int VideoPreviewWidth
/// <remarks>
/// Important: Do not write any logic in dependency property stub.
/// </remarks>
public int VideoPreviewHeight
[TypeConverter(typeof(LengthConverter))]
public double VideoPreviewHeight
{
get
{
return (int)GetValue(VideoPreviewHeightProperty);
return (double)GetValue(VideoPreviewHeightProperty);
}

set
Expand Down Expand Up @@ -209,15 +213,30 @@ public void TakeSnapshotCallback()
playerPoint = this.VideoSourcePlayer.PointToScreen(new Drawing.Point(this.VideoSourcePlayer.ClientRectangle.X, this.VideoSourcePlayer.ClientRectangle.Y));
}

using (var bitmap = new Bitmap(this.VideoPreviewWidth, this.VideoPreviewHeight))
if (double.IsNaN(this.VideoPreviewWidth) || double.IsNaN(this.VideoPreviewHeight))
{
using (var bitmap = new Bitmap((int)this.VideoSourceWindowsFormsHost.ActualWidth, (int)this.VideoSourceWindowsFormsHost.ActualHeight))
{
using (var graphicsFromImage = Graphics.FromImage(bitmap))
{
graphicsFromImage.CopyFromScreen(playerPoint, Drawing.Point.Empty, new Drawing.Size((int)this.VideoSourceWindowsFormsHost.ActualWidth, (int)this.VideoSourceWindowsFormsHost.ActualHeight));
}

this.SnapshotBitmap = new Bitmap(bitmap);
}
}
else
{
using (var bitmap = new Bitmap((int)this.VideoPreviewWidth, (int)this.VideoPreviewHeight))
{
using (var graphicsFromImage = Graphics.FromImage(bitmap))
{
graphicsFromImage.CopyFromScreen(playerPoint, Drawing.Point.Empty, new Drawing.Size(this.VideoPreviewWidth, this.VideoPreviewHeight));
graphicsFromImage.CopyFromScreen(playerPoint, Drawing.Point.Empty, new Drawing.Size((int)this.VideoPreviewWidth, (int)this.VideoPreviewHeight));
}

this.SnapshotBitmap = new Bitmap(bitmap);
}
}
}
catch (Exception exception)
{
Expand Down Expand Up @@ -302,14 +321,22 @@ private static void VideoPreviewWidthPropertyChangedCallback(DependencyObject se
{
return;
}

if (null == eventArgs.NewValue)
{
return;
}

var newValue = (int)eventArgs.NewValue;
webCamDevice.SetVideoPlayerWidth(newValue);
var newValue = (double)eventArgs.NewValue;
if (double.IsNaN(newValue))
{
var parentControl = (webCamDevice.VisualParent as Grid);
webCamDevice.SetVideoPlayerWidth(null != parentControl ? parentControl.Width : newValue);
}
else
{
webCamDevice.SetVideoPlayerWidth(newValue);
}
}

/// <summary>
Expand All @@ -330,8 +357,16 @@ private static void VideoPreviewHeightPropertyChangedCallback(DependencyObject s
return;
}

var newValue = (int)eventArgs.NewValue;
webCamDevice.SetVideoPlayerHeight(newValue);
var newValue = (double)eventArgs.NewValue;
if (double.IsNaN(newValue))
{
var parentControl = (webCamDevice.VisualParent as Grid);
webCamDevice.SetVideoPlayerHeight(null != parentControl ? parentControl.Height : newValue);
}
else
{
webCamDevice.SetVideoPlayerHeight(newValue);
}
}

/// <summary>
Expand Down Expand Up @@ -405,7 +440,7 @@ private void InitializeVideoDevice(string videoDeviceSourceId)
/// Set video player width.
/// </summary>
/// <param name="newWidth">New width value.</param>
private void SetVideoPlayerWidth(int newWidth)
private void SetVideoPlayerWidth(double newWidth)
{
this.NoVideoSourceGrid.Width = newWidth;
this.VideoSourceWindowsFormsHost.Width = newWidth;
Expand All @@ -415,7 +450,7 @@ private void SetVideoPlayerWidth(int newWidth)
/// Set video player height.
/// </summary>
/// <param name="newHeight">New height value.</param>
private void SetVideoPlayerHeight(int newHeight)
private void SetVideoPlayerHeight(double newHeight)
{
this.NoVideoSourceGrid.Height = newHeight;
this.VideoSourceWindowsFormsHost.Height = newHeight;
Expand Down

0 comments on commit 1395166

Please sign in to comment.