Skip to content

Commit

Permalink
person picture
Browse files Browse the repository at this point in the history
  • Loading branch information
sysadminanywhere committed Oct 21, 2023
1 parent 73c5c55 commit d43c529
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/Sysadmin/Controls/MemberOfControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</Grid.RowDefinitions>

<Grid Grid.Row="0">
<TextBlock Text="Member of" Margin="0,4,4,4" HorizontalAlignment="Left" FontWeight="Bold"/>
<TextBlock Text="Member of" Margin="0" HorizontalAlignment="Left" FontWeight="Bold"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button x:Name="addButton" Content="+" Click="addButton_Click"/>
<Button x:Name="deleteButton" Content="-" IsEnabled="False" Click="deleteButton_Click"/>
Expand Down
2 changes: 1 addition & 1 deletion src/Sysadmin/Controls/MembersControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</Grid.RowDefinitions>

<Grid Grid.Row="0">
<TextBlock Text="Members" Margin="0,4,4,4" HorizontalAlignment="Left" FontWeight="Bold" VerticalAlignment="Top"/>
<TextBlock Text="Members" Margin="0" HorizontalAlignment="Left" FontWeight="Bold" VerticalAlignment="Top"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button x:Name="addButton" Content="+" Click="addButton_Click"/>
<Button x:Name="deleteButton" Content="-" IsEnabled="False" Click="deleteButton_Click"/>
Expand Down
22 changes: 22 additions & 0 deletions src/Sysadmin/Controls/PersonPictureControl.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<UserControl xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml" x:Class="Sysadmin.Controls.PersonPictureControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Sysadmin.Controls"
mc:Ignorable="d"
d:DesignHeight="100" d:DesignWidth="100">
<Grid>
<Ellipse Fill="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}" />
<Viewbox>
<TextBlock Margin="4" Text="PP" x:Name="initials" TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
</Viewbox>
<Viewbox>
<Image x:Name="pictureImage" RenderOptions.BitmapScalingMode="HighQuality" Width="100" Height="100">
<Image.Clip>
<EllipseGeometry Center="50,50" RadiusX="50" RadiusY="50" />
</Image.Clip>
</Image>
</Viewbox>
</Grid>
</UserControl>
106 changes: 106 additions & 0 deletions src/Sysadmin/Controls/PersonPictureControl.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Sysadmin.Controls
{
/// <summary>
/// Interaction logic for PersonPictureControl.xaml
/// </summary>
public partial class PersonPictureControl : UserControl
{

public static readonly DependencyProperty DisplayNameProperty = DependencyProperty.Register(
"DisplayName", typeof(string),
typeof(PersonPictureControl)
);

public string DisplayName
{
get => (string)GetValue(DisplayNameProperty);
set
{
SetValue(DisplayNameProperty, value);
initials.Text = GetInitials(value);
}
}

public static readonly DependencyProperty ProfilePictureProperty = DependencyProperty.Register(
"ProfilePicture", typeof(ImageSource),
typeof(PersonPictureControl)
);

public ImageSource ProfilePicture
{
get => (ImageSource)GetValue(ProfilePictureProperty);
set
{
SetValue(ProfilePictureProperty, value);
pictureImage.Source = value;
}
}

private byte[] jpegPhoto;

public byte[] JpegPhoto
{
get { return jpegPhoto; }
set
{
jpegPhoto = value;
ShowPhoto();
}
}

public PersonPictureControl()
{
InitializeComponent();
}

private string GetInitials(string name)
{

if (string.IsNullOrEmpty(name))
return string.Empty;

string[] nameSplit = name.Split(new string[] { ",", " " }, StringSplitOptions.RemoveEmptyEntries);

string initials = "";

foreach (string item in nameSplit)
{
initials += item.Substring(0, 1).ToUpper();
}

return initials;
}

private void ShowPhoto()
{
if (JpegPhoto != null && JpegPhoto.Length > 0)
{
var image = new BitmapImage();
using (var mem = new MemoryStream(JpegPhoto))
{
mem.Position = 0;
image.BeginInit();
image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = null;
image.StreamSource = mem;
image.EndInit();
}
image.Freeze();
pictureImage.Source = image;
}
else
{
pictureImage.Source = null;
}
}

}
}
1 change: 1 addition & 0 deletions src/Sysadmin/Styles.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
</Style>

<Style x:Key="ListDescriptionStyle" TargetType="TextBlock">
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="TextTrimming" Value="CharacterEllipsis"/>
<Setter Property="Margin" Value="6,0,0,6" />
<Setter Property="VerticalAlignment" Value="Center" />
Expand Down
22 changes: 11 additions & 11 deletions src/Sysadmin/Views/Pages/Users/UserPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,19 @@
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<Image Grid.Row="0" Grid.ColumnSpan="2" Source="{Binding ViewModel.User.JpegPhoto, Mode=OneWay, Converter={StaticResource PhotoToSourceConverter}}" x:Name="personPicture" MaxWidth="200" MaxHeight="200" HorizontalAlignment="Left" Margin="0,0,0,8" />
<controls:PersonPictureControl x:Name="personPicture" Grid.Row="0" Grid.ColumnSpan="2" Width="100" Height="100" HorizontalAlignment="Left" Margin="0,0,0,8" DisplayName="{Binding ViewModel.User.DisplayName}" ProfilePicture="{Binding ViewModel.User.JpegPhoto, Mode=OneWay, Converter={StaticResource PhotoToSourceConverter}}" />

<TextBlock Style="{StaticResource TableDetailNameStyle}" Grid.Row="1" Grid.Column="0" Text="Display name" />
<TextBlock Style="{StaticResource TableDetailValueStyle}" Grid.Row="1" Grid.Column="1" Text="{Binding ViewModel.User.DisplayName, Mode=OneWay}" />
Expand Down
29 changes: 8 additions & 21 deletions src/Sysadmin/Views/Pages/Users/UserPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ private void ViewModel_PropertyChanged(object? sender, System.ComponentModel.Pro
{
memberOf.MemberOf = ViewModel.User.MemberOf;
memberOf.PrimaryGroupId = ViewModel.User.PrimaryGroupId;

if(string.IsNullOrEmpty(ViewModel.User.DisplayName))
personPicture.DisplayName = ViewModel.User.CN;
else
personPicture.DisplayName = ViewModel.User.DisplayName;

personPicture.JpegPhoto = ViewModel.User.JpegPhoto;
}
}

Expand All @@ -56,7 +63,7 @@ private async void PhotoMenuItem_Click(object sender, RoutedEventArgs e)
if (openFileDialog.ShowDialog() == true)
{
await ViewModel.UpdatePhoto(ViewModel.User.DistinguishedName, File.ReadAllBytes(openFileDialog.FileName));
ShowPhoto();
personPicture.JpegPhoto = ViewModel.User.JpegPhoto;
}
}
catch (Exception ex)
Expand All @@ -65,26 +72,6 @@ private async void PhotoMenuItem_Click(object sender, RoutedEventArgs e)
}
}

private void ShowPhoto()
{
if (ViewModel.User.JpegPhoto != null && ViewModel.User.JpegPhoto.Length > 0)
{
var image = new BitmapImage();
using (var mem = new MemoryStream(ViewModel.User.JpegPhoto))
{
mem.Position = 0;
image.BeginInit();
image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = null;
image.StreamSource = mem;
image.EndInit();
}
image.Freeze();
personPicture.Source = image;
}
}

private async void MemberOfControl_Changed() //NOSONAR
{
await ViewModel.Get();
Expand Down

0 comments on commit d43c529

Please sign in to comment.