Keyboard.IsKeyToggled


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="170" Width="200">
    <StackPanel HorizontalAlignment="Center">
        <UniformGrid Columns="2">
            <UniformGrid.Resources>
                <Style TargetType="{x:Type CheckBox}">
                    <Setter Property="IsHitTestVisible" Value="False" />
                    <Setter Property="Margin" Value="5" />
                </Style>
            </UniformGrid.Resources>

        </UniformGrid>
        <Button Content="Check Keyboard" Margin="10" Click="Button_Click"/>
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            CheckKeyboardState();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            CheckKeyboardState();
        }
        private void CheckKeyboardState()
        {
            Console.WriteLine(Keyboard.IsKeyToggled(Key.NumLock));
        }
    }
}

   
    
     


Is Key.CapsLock Toggled


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="170" Width="200">
    <StackPanel HorizontalAlignment="Center">
        <UniformGrid Columns="2">
            <UniformGrid.Resources>
                <Style TargetType="{x:Type CheckBox}">
                    <Setter Property="IsHitTestVisible" Value="False" />
                    <Setter Property="Margin" Value="5" />
                </Style>
            </UniformGrid.Resources>

        </UniformGrid>
        <Button Content="Check Keyboard" Margin="10" Click="Button_Click"/>
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            CheckKeyboardState();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            CheckKeyboardState();
        }
        private void CheckKeyboardState()
        {
            Console.WriteLine(Keyboard.IsKeyToggled(Key.CapsLock));
        }
    }
}

   
    
     


Query Left / Right Shift key


   
  
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="170" Width="200">
    <StackPanel HorizontalAlignment="Center">
        <UniformGrid Columns="2">
            <UniformGrid.Resources>
                <Style TargetType="{x:Type CheckBox}">
                    <Setter Property="IsHitTestVisible" Value="False" />
                    <Setter Property="Margin" Value="5" />
                </Style>
            </UniformGrid.Resources>

        </UniformGrid>
        <Button Content="Check Keyboard" Margin="10" Click="Button_Click"/>
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            CheckKeyboardState();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            CheckKeyboardState();
        }
        private void CheckKeyboardState()
        {
            Console.WriteLine(Keyboard.IsKeyDown(Key.LeftShift));
            Console.WriteLine(Keyboard.IsKeyDown(Key.RightShift));
        }
    }
}

   
    
     


Query Left / Right control key


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="170" Width="200">
    <StackPanel HorizontalAlignment="Center">
        <UniformGrid Columns="2">
            <UniformGrid.Resources>
                <Style TargetType="{x:Type CheckBox}">
                    <Setter Property="IsHitTestVisible" Value="False" />
                    <Setter Property="Margin" Value="5" />
                </Style>
            </UniformGrid.Resources>

        </UniformGrid>
        <Button Content="Check Keyboard" Margin="10" Click="Button_Click"/>
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            CheckKeyboardState();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            CheckKeyboardState();
        }
        private void CheckKeyboardState()
        {
            Console.WriteLine(Keyboard.IsKeyDown(Key.LeftCtrl));
            Console.WriteLine(Keyboard.IsKeyDown(Key.RightCtrl));
        }
    }
}

   
    
     


Use JpegBitmapDecoder

   
 
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Media.Imaging;
using System.IO;

    class UseBitmapCodecs
    {
        static string GetCamera(string myJpegPath)
        {
            JpegBitmapDecoder decoder = new JpegBitmapDecoder(new Uri(myJpegPath),BitmapCreateOptions.None, BitmapCacheOption.None);
            BitmapMetadata bmpData = (BitmapMetadata) decoder.Frames[0].Metadata;
            return bmpData.CameraModel;
        }
    }

   
     


Write Jpeg file from BitmapSource

   
 
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Media.Imaging;
using System.IO;

    class UseBitmapCodecs
    {
        static void WriteJpeg(string fileName, int quality, BitmapSource bmp)
        {

            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            BitmapFrame outputFrame = BitmapFrame.Create(bmp);
            encoder.Frames.Add(outputFrame);
            encoder.QualityLevel = quality;

            using (FileStream file = File.OpenWrite(fileName))
            {
                encoder.Save(file);
            }
        }
    }

   
     


Color Converter

   
 
//http://aspascension.codeplex.com/
//Microsoft Public License (Ms-PL)

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;
using System.Windows.Data;

namespace ASPAscension.Silverlight.Converters
{
    public class ColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value.GetType() == typeof(String))
            {
                string val = value as string;
                Color c;
                if (val.StartsWith("#"))
                {
                    val = val.Replace("#", "");  
                    byte a = System.Convert.ToByte("ff", 16);  
                    byte pos = 0;  
                    if (val.Length == 8)  
                    {  
                        a = System.Convert.ToByte(val.Substring(pos, 2), 16);  
                        pos = 2;  
                    }  
                    byte r = System.Convert.ToByte(val.Substring(pos, 2), 16);  
                    pos += 2;  
                    byte g = System.Convert.ToByte(val.Substring(pos, 2), 16);  
                    pos += 2;  
                    byte b = System.Convert.ToByte(val.Substring(pos, 2), 16);  
                    c = Color.FromArgb(a, r, g, b);  
                    return new SolidColorBrush(c);
                }
                else
                {
                    try
                    {
                        c = GetColorFromString(value as string);
                        return new SolidColorBrush(c);
                    }
                    catch (InvalidCastException ex)
                    {
                        return null;
                    }
                }
            }
            return null;
        }

        public static Color GetColorFromString(string colorString)
        {
            Type colorType = (typeof(System.Windows.Media.Colors));
            if (colorType.GetProperty(colorString) != null)
            {
                object color = colorType.InvokeMember(colorString, BindingFlags.GetProperty, null, null, null);
                try
                {
                    return (Color)color;
                }
                catch
                {
                    throw new InvalidCastException("Color not defined");
                }
            }
            return Colors.Transparent;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            SolidColorBrush val = value as SolidColorBrush;
            return val.Color.ToString();
            //if (typeof(Colors).GetProperty(val.Color.ToString()) != null)
            //    return typeof(Colors).GetProperty(val.Color.ToString()).GetValue(val, null);
            //else
            //    return "#" + val.Color.A.ToString() + val.Color.R.ToString() + val.Color.G.ToString() + val.Color.B.ToString();
        }
    }
}