c# wpf 利用截屏键实现截屏功能

本文介绍了如何在C# WPF应用程序中实现截屏功能,通过模拟截屏键并结合从剪贴板获取图片,解决了一些主题下窗口透明的问题,提供了一种高效截屏的方法。
摘要由CSDN通过智能技术生成

    最近做一个wpf程序需要截图功能,查找资料费了一些曲折,跟大家分享一下。

    先是找到了这样一份代码:

    static class ScreenCut

    {

        public static System.Drawing.Bitmap GetScreenSnapshot()

        {

            System.Drawing.Rectangle rc = System.Windows.Forms.SystemInformation.VirtualScreen;

            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(rc.Width, rc.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))

            {

                g.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, System.Drawing.CopyPixelOperation.SourceCopy);

            }

            return bitmap;

        }

        public static BitmapSource ToBitmapSource(this System.Drawing.Bitmap bmp)

        {

            BitmapSource returnSource;

            try

            {

                returnSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

            }

            catch

            {

                returnSource = null;

            }

            return returnSource;

        }

 

        public static BitmapSource CopyFromScreenSnapshot(BitmapSource screenSnapshot, Rect region)

        {

      

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值