WpfClipboardMonitor

WPF Clipboard Monitor is written in pure WPF. It does not use any Forms library. It can be used in a lot of different ways.

Getting Started

To get started, add the WpfClipboardMonitor package to your project.

Examples

Example: Clipboard Monitor as member

The ClipboardMonitor class can be used as a member of your main window.

using System;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using WpfClipboardMonitor;

namespace ImageCatcher
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private string path;
        private ClipboardMonitor clipboardMonitor;

        public MainWindow()
        {
            InitializeComponent();

            this.path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "Clipboard");
            Directory.CreateDirectory(this.path);

            this.clipboardMonitor = new ClipboardMonitor(this);
            this.clipboardMonitor.ClipboardUpdate += OnClipboardUpdate;
        }

        private void OnClipboardUpdate(object sender, EventArgs e)
        {
            try
            {
                if (Clipboard.ContainsImage())
                {
                    string filePath = Path.Combine(this.path, $"Image_{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")}.jpg");
                    using (var file = File.Create(filePath))
                    {
                        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(Clipboard.GetImage()));
                        encoder.Save(file);
                    }
                }
            }
            catch
            { }
        }
    }
}

Example: Clipboard Monitor as window derived class with event

The ClipboardMonitarWindow class can be used as the base of your MainWindow with event handling.

<ClipboardMonitorWindow x:Class="ImageCatcher.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ImageCatcher"
        mc:Ignorable="d"
        Title="Image Catcher" Height="70" Width="260" Icon="Camera.ico"
        ClipboardUpdate="ClipboardMonitorWindow_ClipboardUpdate">
    <Grid>
        
    </Grid>
</ClipboardMonitorWindow>
using System;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using WpfClipboardMonitor;

namespace ImageCatcher
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : ClipboardMonitorWindow
    {
        private string path;

        public MainWindow()
        {
            InitializeComponent();

            this.path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "Clipboard");
            Directory.CreateDirectory(this.path);

        }

        private void ClipboardMonitorWindow_ClipboardUpdate(object sender, EventArgs e)
        {
            try
            { 
                if (Clipboard.ContainsImage())
                {
                    string filePath = Path.Combine(this.path, $"Image_{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")}.jpg");
                    using (var file = File.Create(filePath))
                    {
                        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(Clipboard.GetImage()));
                        encoder.Save(file);
                    }
                }
            }
            catch
            { }
        }
    }
}

Example: Clipboard Monitor as window derived class with method override

The ClipboardMonitarWindow class can be used as the base of your MainWindow with a overwritten method.

<ClipboardMonitorWindow x:Class="ImageCatcher.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ImageCatcher"
        mc:Ignorable="d"
        Title="Image Catcher" Height="70" Width="260" Icon="Camera.ico">
    <Grid>
        
    </Grid>
</ClipboardMonitorWindow>
using System;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using WpfClipboardMonitor;

namespace ImageCatcher
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : ClipboardMonitorWindow
    {
        private string path;

        public MainWindow()
        {
            InitializeComponent();

            this.path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "Clipboard");
            Directory.CreateDirectory(this.path);
        }

        protected override void OnClipboardUpdate()
        {
            try
            {
                if (Clipboard.ContainsImage())
                {
                    string filePath = Path.Combine(this.path, $"Image_{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")}.jpg");
                    using (var file = File.Create(filePath))
                    {
                        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(Clipboard.GetImage()));
                        encoder.Save(file);
                    }
                }
            }
            catch
            { }
        }
    }
}

Example: Clipboard Monitor as window derived class in a MVVM environment

The ClipboardMonitarWindow class can be used as the base of your MainWindow with a command for MVVM architecture.

<ClipboardMonitorWindow x:Class="ImageCatcher.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ImageCatcher"
        mc:Ignorable="d"
        Title="Image Catcher" Height="70" Width="260" Icon="Camera.ico"
        ClipboardUpdateCommand="{Binding ClipboardUpdateCommand}">
    <Window.DataContext>
        <local:MainViewModel/>
    </Window.DataContext>
    <Grid>
        
    </Grid>
</ClipboardMonitorWindow>
using System;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;

namespace ImageCatcher
{
    public class MainViewModel
    {
        private string path;

        public DelegateCommand ClipboardUpdateCommand { get; private set; }

        public MainViewModel()
        {
            this.ClipboardUpdateCommand = new DelegateCommand(OnClipboardUpdate, OnCanClipboardUpdate);

            this.path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "Clipboard");
            Directory.CreateDirectory(this.path);
        }

        public void OnClipboardUpdate()
        {
            try
            {
                if (Clipboard.ContainsImage())
                {
                    string filePath = Path.Combine(this.path, $"Image_{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")}.jpg");
                    using (var file = File.Create(filePath))
                    {
                        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(Clipboard.GetImage()));
                        encoder.Save(file);
                    }
                }
            }
            catch
            { }
        }

        public bool OnCanClipboardUpdate()
        {
            return true;
        }
    }
}
Donate