Skip to content

ScreenCapture

闫驚鏵(Jinhua Yan) edited this page Nov 24, 2024 · 7 revisions
       var screenCaptureExt = new ScreenCaptureExt();
       screenCaptureExt.SnapCanceled += ScreenCaptureExt_SnapCanceled;
       screenCaptureExt.SnapCompleted += ScreenCaptureExt_SnapCompleted;

        private void ScreenCaptureExt_SnapCompleted(System.Windows.Media.Imaging.BitmapSource bitmap)
        {
           //Completed
        }

        private void ScreenCaptureExt_SnapCanceled()
        {
           //Canceled
        }

CSharp

Application.Current.Dispatcher.Invoke(new Action(delegate
{
    var screenCapturer = new ScreenCapture();
    screenCapturer.SnapCompleted += ScreenCapturer_SnapCompleted;
    screenCapturer.SnapCanceled += ScreenCapturer_SnapCanceled;
    screenCapturer.Capture();
}));
 private void ScreenCapturer_SnapCanceled()
 {
    //Canceled
 }

private void ScreenCapturer_SnapCompleted(CroppedBitmap bitmap)
{
   //Completed
}

When the loaded resource dictionary cannot be written in App.xaml, ScreenCapture and ScreenCut cannot be used normally when written in Window.Resources.

Solution 1

Introduce resources to the current page

<Window
    x:Class="WpfScreenCapture.ScreenCaptureWindow"
    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:local="clr-namespace:WpfScreenCapture"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:wd="https://github.com/WPFDevelopersOrg/WPFDevelopers"
    Title="Window1"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/WPFDevelopers;component/Themes/Light.Blue.xaml" />
                <!-- 需要注意 wd:Resources 必须在配色主题后,Theme="Dark" 为黑色皮肤 -->
                <wd:Resources Theme="Light" />
                <ResourceDictionary Source="pack://application:,,,/WPFDevelopers;component/Themes/Theme.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <Button
        Margin="0,10"
        HorizontalAlignment="Center"
        VerticalAlignment="Top"
        Click="Button_Click"
        Content="ScreenCapture" />
    
</Window>

Send its own Resources when showing the ScreenCapture control.

private void Button_Click(object sender, RoutedEventArgs e)
{
  Dispatcher.Invoke(new Action(delegate
  {
     ScreenCapture screenCapturer = new ScreenCapture(resources: this.Resources);
     screenCapturer.Capture();
  }));
}

Solution 2

Start ScreenCapture in process mode

private void Button_Click(object sender, RoutedEventArgs e)
{
  ScreenCaptureExt screenCaptureExt = new ScreenCaptureExt();
  screenCaptureExt.SnapCanceled += ScreenCaptureExt_SnapCanceled;
  screenCaptureExt.SnapCompleted += ScreenCaptureExt_SnapCompleted;
}