Screenshot using WPF development program, so easy! Screenshot using WPF development program, so easy!

Excerpt: https://www.cnblogs.com/yuanchenhui/p/screenshot-easy.html

Screenshot using WPF development program, so easy!

 

Introduction   QQ, micro letter screenshot function has been very strong, did not seem necessary to develop a screenshot of the program. But sometimes QQ hotkey is occupied, not quickly open screenshots; sometimes, every day hanging QQ, leadership is not happy. Since it is a programmer, it is necessary to develop their own screen capture tools, features, arbitrary, not Yoshiya.

Stress again: is productivity tool! Before did not grasp WPF, I will not develop such a program, if the MFC, winform framework, the workload is quite large, developed the effects are relatively low. I use WPF, it took more than a day of effort, developed this little program. Positioning program is simple functions, usually work out of the way, by the time, a key shot!

 Interface   program execution Download:  a key shots, the point I downloaded.

In order not to affect the visual, the program's main interface is very small. The program will show in the forefront of all interfaces.

There are two buttons 1) "quick shots": After the shots, shots immediately copied to the clipboard. 2) "edit screenshot +": the theme, and text can be labeled arrows on FIG.

It is being shot, the effect of:

After the shot, you can edit:

 Seemingly simple, to develop skills demanding. Experts see Road!

Development of ideas

  As the saying goes: Seeing is not necessarily true. Even thus developed. Program called the screenshots, you do not think about how the strength to intercept another window pattern, must be very strenuous! Thinking is to deceive the public: first copy the entire screen, put it in your program form, the form is maximized to fill the screen! Users still see the entire screen, but the entire screen has been perpetrating a fraud! After that, all your operations are on your own form processing, of course, you may oyster!

Interception of the entire screen

Copy the code
       public Bitmap GetScreenSnapshot()
        {
            System.Drawing.Rectangle rc = SystemInformation.VirtualScreen;
            var bitmap = new Bitmap(rc.Width, rc.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            using (Graphics memoryGrahics = Graphics.FromImage(bitmap))
            {
                memoryGrahics.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy);
            }

            return bitmap;
        }
Copy the code

Create a full-screen window

 Note that the form property, so as to fully cover the entire screen.

Layer Layout

This is useful tips! In order to achieve non-Screenshot region shadow effects, costs a lot of effort! Even so, the feeling than winform with handy!

Note: I have not been to belittle winform, but have to admit that these two things are not a product of the times. wpf design ideas advanced than many winform. Just wpf new concept, but few people use, often being developed Meng ring! After a period of confusion, the future will be bright!

Layout window, not much to say! Directly on the code. I made a note of the code!

Copy the code
<Window.Resources>
        <ControlTemplate x:Key="templatePushButton" TargetType="RadioButton">
            <Border x:Name="Part_Border" BorderThickness="1" BorderBrush="Gray" 
                                        Background="{TemplateBinding Background}"
                                        Margin="{TemplateBinding Margin}"
                                        Padding="{TemplateBinding Padding}">
                <ContentPresenter></ContentPresenter>
            </Border>

            <ControlTemplate.Triggers >
                <Trigger Property="IsChecked" Value="True">
                    <Setter  TargetName="Part_Border"  Property="BorderBrush" Value="Blue"></Setter>
                </Trigger>
        <Image the X-: the Name = "imgScreen"
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter  TargetName="Part_Border"  Property="Background" Value="#FFb2dff9"></Setter>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

        <Style x:Key="stylePushButton" TargetType="RadioButton">
            <Setter Property="VerticalAlignment" Value="Center"></Setter>
            <Setter Property="Padding" Value="8,5,8,5"></Setter>
            <Setter Property="Template" Value="{StaticResource templatePushButton}"></Setter>
        </Style>

    </Window.Resources>
    <Background = Grid "Green"> 
               the MouseDown = "ImgScreen_MouseDown"
        <-! Entire screen image ->
               = The MouseUp "ImgScreen_MouseUp" 
               the Stretch = "None"            
               the MouseMove = "ImgScreen_MouseMove"> 
        </ Image> 

        <- covered with a black, translucent ->! 
        <X the Grid: the Name = "gridCover" the Visibility = "Collapsed" Background = "Black" the Opacity = "0.5"> 
        </ the Grid> 

        <the Grid> 
            <Grid.RowDefinitions> 
                <the RowDefinition the Height = "*"> </ the RowDefinition> 
                <the RowDefinition the Height = "Auto"> </ the RowDefinition> 
            </ the Grid. RowDefinitions> 
            <-! front covered by a layer of black, but the taken image does not cover, and then only where the display clipped image ->
            <the Grid X: the Name = "gridCutImg"  
                  MouseDown="ImgCut_MouseDown" 
                  MouseMove="ImgCut_MouseMove"
                   MouseUp="ImgCut_MouseUp">
                <Image x:Name="imgCut" Grid.RowSpan="3" Stretch="None"                                                                    
                   HorizontalAlignment="Left" VerticalAlignment="Top"></Image>
                <!--用来画箭头和文字-->
                <Canvas x:Name="canvasEdit"  HorizontalAlignment='Left'
                        VerticalAlignment="Top"  Background="Transparent">
                    
                </Canvas>
            </Grid>

            <!--显示提示信息-->
            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center " 
                           FontSize="22" Foreground="Yellow"
                          Opacity =" 0.8 "> sliding the mouse began screenshots screenshots saved to the clipboard, press the ESC key to exit </ TextBlock>
 
            <Grid.RowSpan the StackPanel = ". 3"> 
                <the Grid> 
                    <Grid.ColumnDefinitions> 
                        <the ColumnDefinition the Width = "Auto"> </ the ColumnDefinition> 
                        <the ColumnDefinition the Width = "*"> </ the ColumnDefinition> 
                    </Grid.ColumnDefinitions> 
                </ the Grid> 
                <- - indication frame theme!> 
                <Border X: the Name = "borderSelect" 
                the HorizontalAlignment = "Left" VerticalAlignment = "Top"
                = BorderThickness ". 1" the BorderBrush = "Red"> </ Border> 
                <-! Indicate the width and height ->
                <Grid  HorizontalAlignment="Stretch" >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="auto"></ColumnDefinition>
                        <ColumnDefinition Width="auto"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <TextBlock x:Name="txtCutInfo"  Padding="8,5,8,5" 
                               HorizontalAlignment="Left"
                              Background="White" VerticalAlignment="Center">1</TextBlock>
                    <StackPanel x:Name="stackEdit" Orientation="Horizontal" HorizontalAlignment="Right" Grid.Column="1" Margin="2">
                        <RadioButton x:Name="radioArrow" GroupName="editType" Click="RadioArrow_Click" Foreground="Black" Padding="10,5,10,5" Style="{StaticResource stylePushButton}">↑</RadioButton>
                        <RadioButton  x:Name="radioText"  GroupName="editType" Click="RadioText_Click" Foreground="Black" Style="{StaticResource stylePushButton}">文</RadioButton>
                        <RadioButton  x:Name="radioClose" Click="RadioClose_Click" Foreground="Red" Style="{StaticResource stylePushButton}">X</RadioButton>
                    </StackPanel>
                </Grid>

            </StackPanel>

        </Grid>
    </Grid>
Copy the code

When the mouse is moved, the selected area is calculated continuously provided borderSelect properties.

Copy the code
 private void ImgScreen_MouseMove(object sender, MouseEventArgs e)
        {
            if (!_isMouseDown)
                return;

            gridCover.Visibility = Visibility.Visible;

            //计算鼠标选中区域
            Point currentPoint = e.GetPosition(imgScreen);
            Point borderPoint = e.GetPosition(borderSelect);

            double xDelta = xDelta_BoderToImgScreen;
            double yDelta = yDelta_BoderToImgScreen;

            _rectImgCut = ImageHelper.ToRect(currentPoint, _startPoint);

            Rect rectBoderCut = ImageHelper.ToRect(new Point(currentPoint.X + xDelta, currentPoint.Y + yDelta),
                new Point(_startPoint.X + xDelta, _startPoint.Y + yDelta));

            //设置方框位置和大小
            Thickness thickness = new Thickness(rectBoderCut.Left, rectBoderCut.Top, 0, 0);
            borderSelect.SetValue(FrameworkElement.MarginProperty, thickness);
            imgCut.SetValue(FrameworkElement.MarginProperty, thickness);

            thickness = new Thickness(rectBoderCut.Left, 3, 0, 0);
            txtCutInfo.SetValue(FrameworkElement.MarginProperty, thickness);

            borderSelect.Width = Math.Abs(_startPoint.X - currentPoint.X);
            borderSelect.Height = Math.Abs(_startPoint.Y - currentPoint.Y);
            borderSelect.Visibility = Visibility.Visible;

            In order to prevent the entire FIG // darkened matting mouse to select regions of the image, then the image displayed on the upper 
            imgCut.Source GetBitmapCut = (); 

            Int32Rect imgDestRect GetCutRect = (); 
            txtCutInfo.Text string.Format = ($ "width: { high imgDestRect.Width}: {imgDestRect.Height} "); 
        }
Copy the code

This, the main program logic solution! Small but complete! Wpf layout techniques used here are many. These processing techniques and winform consider the difference is great! wpf Although bitter and difficult, feeling into a waiting gate like the deep sea! If you stick with it, you will feel suddenly see the light, but also understand Microsoft's painstaking!

The program runs very similar effect QQ screenshot of. Along the thoughtful move forward, we can develop and QQ screenshot same effect!

Focus on .NET, VC ++. Good at WPF, WinForm, Socket technology. Technical exchange QQ 13712486
 
Category:  c # WPF image
Tags:  WPF Screen c #

Introduction   QQ, micro letter screenshot function has been very strong, did not seem necessary to develop a screenshot of the program. But sometimes QQ hotkey is occupied, not quickly open screenshots; sometimes, every day hanging QQ, leadership is not happy. Since it is a programmer, it is necessary to develop their own screen capture tools, features, arbitrary, not Yoshiya.

Stress again: is productivity tool! Before did not grasp WPF, I will not develop such a program, if the MFC, winform framework, the workload is quite large, developed the effects are relatively low. I use WPF, it took more than a day of effort, developed this little program. Positioning program is simple functions, usually work out of the way, by the time, a key shot!

 Interface   program execution Download:  a key shots, the point I downloaded.

In order not to affect the visual, the program's main interface is very small. The program will show in the forefront of all interfaces.

There are two buttons 1) "quick shots": After the shots, shots immediately copied to the clipboard. 2) "edit screenshot +": the theme, and text can be labeled arrows on FIG.

It is being shot, the effect of:

After the shot, you can edit:

 Seemingly simple, to develop skills demanding. Experts see Road!

Development of ideas

  As the saying goes: Seeing is not necessarily true. Even thus developed. Program called the screenshots, you do not think about how the strength to intercept another window pattern, must be very strenuous! Thinking is to deceive the public: first copy the entire screen, put it in your program form, the form is maximized to fill the screen! Users still see the entire screen, but the entire screen has been perpetrating a fraud! After that, all your operations are on your own form processing, of course, you may oyster!

Interception of the entire screen

Copy the code
       public Bitmap GetScreenSnapshot()
        {
            System.Drawing.Rectangle rc = SystemInformation.VirtualScreen;
            var bitmap = new Bitmap(rc.Width, rc.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            using (Graphics memoryGrahics = Graphics.FromImage(bitmap))
            {
                memoryGrahics.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy);
            }

            return bitmap;
        }
Copy the code

Create a full-screen window

 Note that the form property, so as to fully cover the entire screen.

Layer Layout

This is useful tips! In order to achieve non-Screenshot region shadow effects, costs a lot of effort! Even so, the feeling than winform with handy!

Note: I have not been to belittle winform, but have to admit that these two things are not a product of the times. wpf design ideas advanced than many winform. Just wpf new concept, but few people use, often being developed Meng ring! After a period of confusion, the future will be bright!

Layout window, not much to say! Directly on the code. I made a note of the code!

Copy the code
<Window.Resources>
        <ControlTemplate x:Key="templatePushButton" TargetType="RadioButton">
            <Border x:Name="Part_Border" BorderThickness="1" BorderBrush="Gray" 
                                        Background="{TemplateBinding Background}"
                                        Margin="{TemplateBinding Margin}"
                                        Padding="{TemplateBinding Padding}">
                <ContentPresenter></ContentPresenter>
            </Border>

            <ControlTemplate.Triggers >
                <Trigger Property="IsChecked" Value="True">
                    <Setter  TargetName="Part_Border"  Property="BorderBrush" Value="Blue"></Setter>
                </Trigger>
        <Image the X-: the Name = "imgScreen"
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter  TargetName="Part_Border"  Property="Background" Value="#FFb2dff9"></Setter>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

        <Style x:Key="stylePushButton" TargetType="RadioButton">
            <Setter Property="VerticalAlignment" Value="Center"></Setter>
            <Setter Property="Padding" Value="8,5,8,5"></Setter>
            <Setter Property="Template" Value="{StaticResource templatePushButton}"></Setter>
        </Style>

    </Window.Resources>
    <Background = Grid "Green"> 
               the MouseDown = "ImgScreen_MouseDown"
        <-! Entire screen image ->
               = The MouseUp "ImgScreen_MouseUp" 
               the Stretch = "None"            
               the MouseMove = "ImgScreen_MouseMove"> 
        </ Image> 

        <- covered with a black, translucent ->! 
        <X the Grid: the Name = "gridCover" the Visibility = "Collapsed" Background = "Black" the Opacity = "0.5"> 
        </ the Grid> 

        <the Grid> 
            <Grid.RowDefinitions> 
                <the RowDefinition the Height = "*"> </ the RowDefinition> 
                <the RowDefinition the Height = "Auto"> </ the RowDefinition> 
            </ the Grid. RowDefinitions> 
            <-! front covered by a layer of black, but the taken image does not cover, and then only where the display clipped image ->
            <the Grid X: the Name = "gridCutImg"  
                  MouseDown="ImgCut_MouseDown" 
                  MouseMove="ImgCut_MouseMove"
                   MouseUp="ImgCut_MouseUp">
                <Image x:Name="imgCut" Grid.RowSpan="3" Stretch="None"                                                                    
                   HorizontalAlignment="Left" VerticalAlignment="Top"></Image>
                <!--用来画箭头和文字-->
                <Canvas x:Name="canvasEdit"  HorizontalAlignment='Left'
                        VerticalAlignment="Top"  Background="Transparent">
                    
                </Canvas>
            </Grid>

            <!--显示提示信息-->
            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center " 
                           FontSize="22" Foreground="Yellow"
                          Opacity =" 0.8 "> sliding the mouse began screenshots screenshots saved to the clipboard, press the ESC key to exit </ TextBlock>
 
            <Grid.RowSpan the StackPanel = ". 3"> 
                <the Grid> 
                    <Grid.ColumnDefinitions> 
                        <the ColumnDefinition the Width = "Auto"> </ the ColumnDefinition> 
                        <the ColumnDefinition the Width = "*"> </ the ColumnDefinition> 
                    </Grid.ColumnDefinitions> 
                </ the Grid> 
                <- - indication frame theme!> 
                <Border X: the Name = "borderSelect" 
                the HorizontalAlignment = "Left" VerticalAlignment = "Top"
                = BorderThickness ". 1" the BorderBrush = "Red"> </ Border> 
                <-! Indicate the width and height ->
                <Grid  HorizontalAlignment="Stretch" >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="auto"></ColumnDefinition>
                        <ColumnDefinition Width="auto"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <TextBlock x:Name="txtCutInfo"  Padding="8,5,8,5" 
                               HorizontalAlignment="Left"
                              Background="White" VerticalAlignment="Center">1</TextBlock>
                    <StackPanel x:Name="stackEdit" Orientation="Horizontal" HorizontalAlignment="Right" Grid.Column="1" Margin="2">
                        <RadioButton x:Name="radioArrow" GroupName="editType" Click="RadioArrow_Click" Foreground="Black" Padding="10,5,10,5" Style="{StaticResource stylePushButton}">↑</RadioButton>
                        <RadioButton  x:Name="radioText"  GroupName="editType" Click="RadioText_Click" Foreground="Black" Style="{StaticResource stylePushButton}">文</RadioButton>
                        <RadioButton  x:Name="radioClose" Click="RadioClose_Click" Foreground="Red" Style="{StaticResource stylePushButton}">X</RadioButton>
                    </StackPanel>
                </Grid>

            </StackPanel>

        </Grid>
    </Grid>
Copy the code

When the mouse is moved, the selected area is calculated continuously provided borderSelect properties.

Copy the code
 private void ImgScreen_MouseMove(object sender, MouseEventArgs e)
        {
            if (!_isMouseDown)
                return;

            gridCover.Visibility = Visibility.Visible;

            //计算鼠标选中区域
            Point currentPoint = e.GetPosition(imgScreen);
            Point borderPoint = e.GetPosition(borderSelect);

            double xDelta = xDelta_BoderToImgScreen;
            double yDelta = yDelta_BoderToImgScreen;

            _rectImgCut = ImageHelper.ToRect(currentPoint, _startPoint);

            Rect rectBoderCut = ImageHelper.ToRect(new Point(currentPoint.X + xDelta, currentPoint.Y + yDelta),
                new Point(_startPoint.X + xDelta, _startPoint.Y + yDelta));

            //设置方框位置和大小
            Thickness thickness = new Thickness(rectBoderCut.Left, rectBoderCut.Top, 0, 0);
            borderSelect.SetValue(FrameworkElement.MarginProperty, thickness);
            imgCut.SetValue(FrameworkElement.MarginProperty, thickness);

            thickness = new Thickness(rectBoderCut.Left, 3, 0, 0);
            txtCutInfo.SetValue(FrameworkElement.MarginProperty, thickness);

            borderSelect.Width = Math.Abs(_startPoint.X - currentPoint.X);
            borderSelect.Height = Math.Abs(_startPoint.Y - currentPoint.Y);
            borderSelect.Visibility = Visibility.Visible;

            In order to prevent the entire FIG // darkened matting mouse to select regions of the image, then the image displayed on the upper 
            imgCut.Source GetBitmapCut = (); 

            Int32Rect imgDestRect GetCutRect = (); 
            txtCutInfo.Text string.Format = ($ "width: { high imgDestRect.Width}: {imgDestRect.Height} "); 
        }
Copy the code

This, the main program logic solution! Small but complete! Wpf layout techniques used here are many. These processing techniques and winform consider the difference is great! wpf Although bitter and difficult, feeling into a waiting gate like the deep sea! If you stick with it, you will feel suddenly see the light, but also understand Microsoft's painstaking!

The program runs very similar effect QQ screenshot of. Along the thoughtful move forward, we can develop and QQ screenshot same effect!

Guess you like

Origin www.cnblogs.com/xcj26/p/11711436.html