WPF窗体截图与保存

窗体CS文件里的代码

public BitmapSource GetBitmapSource(int x, int y, int width, int height)
        {
            //var scaleWidth = (screenWidth * 1.0) / SystemParameters.PrimaryScreenWidth;
            //var scaleHeight = (screenHeight * 1.0) / SystemParameters.PrimaryScreenHeight;
            var scaleWidth = 1.0;
            var scaleHeight = 1.0;
            var w = (int)(width * scaleWidth);
            var h = (int)(height * scaleHeight);
            var l = (int)(x * scaleWidth);
            var t = (int)(y * scaleHeight);
            using (var bm = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
            {
                using (var g = Graphics.FromImage(bm))
                {
                    g.CopyFromScreen(l, t, 0, 0, bm.Size);
                    return Imaging.CreateBitmapSourceFromHBitmap(
                        bm.GetHbitmap(),
                        IntPtr.Zero,
                        Int32Rect.Empty,
                        BitmapSizeOptions.FromEmptyOptions());
                }
            }
        }
private BitmapSource GetCapture()

        {
            int left = (int)this.Left; // 获取窗体在屏幕中的左边缘位置
            int top = (int)this.Top; // 获取窗体在屏幕中的上边缘位置
            int width = (int)this.Width; // 获取窗体的宽度
            int height = (int)this.Height; // 获取窗体的高度
            return GetBitmapSource(left, top, width, height) ;
        }
public void SaveToPng(BitmapSource image, string fileName) {

            using (var fs = System.IO.File.Create(fileName))
            {
                BitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(image));
                encoder.Save(fs);
            }
        }
public void ScreenCapture() 
        {
            string path = "C:\\Users\\Administrator\\Documents\\123.png";
            var source = GetCapture();

            Clipboard.SetImage(source);
            SaveToPng(source, path);
         }
using materialtest.ViewModel;
using materialtest.Views;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using static System.Net.Mime.MediaTypeNames;
using static System.Windows.Forms.AxHost;
using Image = System.Windows.Controls.Image;
using Rectangle = System.Windows.Shapes.Rectangle;

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF(Windows Presentation Foundation)是一种用于创建用户界面的框架,它提供了丰富的功能和灵活性。在WPF中,可以使用鼠标事件来响应用户的鼠标操作。 WPF窗体中的鼠标事件包括以下几种: 1. MouseDown:当鼠标按下时触发的事件。 2. MouseUp:当鼠标释放时触发的事件。 3. MouseMove:当鼠标移动时触发的事件。 4. MouseEnter:当鼠标进入窗体区域时触发的事件。 5. MouseLeave:当鼠标离开窗体区域时触发的事件。 6. MouseDoubleClick:当鼠标双击时触发的事件。 要使用这些鼠标事件,可以在XAML中为窗体或其他UI元素添加相应的事件处理程序,也可以在代码中通过订阅事件来实现。 以下是一个示例,演示如何在WPF窗体中处理鼠标事件: ```xaml <Window x:Class="WpfApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPF Mouse Events" Height="450" Width="800" MouseDown="Window_MouseDown" MouseUp="Window_MouseUp" MouseMove="Window_MouseMove" MouseEnter="Window_MouseEnter" MouseLeave="Window_MouseLeave" MouseDoubleClick="Window_MouseDoubleClick"> <Grid> <!-- 窗体内容 --> </Grid> </Window> ``` ```csharp public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_MouseDown(object sender, MouseButtonEventArgs e) { // 鼠标按下事件处理逻辑 } private void Window_MouseUp(object sender, MouseButtonEventArgs e) { // 鼠标释放事件处理逻辑 } private void Window_MouseMove(object sender, MouseEventArgs e) { // 鼠标移动事件处理逻辑 } private void Window_MouseEnter(object sender, MouseEventArgs e) { // 鼠标进入窗体事件处理逻辑 } private void Window_MouseLeave(object sender, MouseEventArgs e) { // 鼠标离开窗体事件处理逻辑 } private void Window_MouseDoubleClick(object sender, MouseButtonEventArgs e) { // 鼠标双击事件处理逻辑 } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值