it编程 > 编程语言 > Asp.net

基于C#实现Windows桌面截图功能

32人参与 2025-05-12 Asp.net

前言

知识梳理

实现功能:

实现步骤:

运行效果

代码

主窗体代码

public partial class mainform : form
{
    public mainform()
    {
        initializecomponent();
        this.centertoparent();
        this.maximumsize = this.size;
        this.minimumsize = this.size;
    }
    private void btn_screenshot_click(object sender, eventargs e)
    {
        this.hide();
        thread.sleep(500);
        screenshot();
    }
    /// 截图方法
    private void screenshot()
    {
        try
        {
            // 获取屏幕尺寸
            rectangle bounds = screen.getbounds(point.empty);
            // 创建位图对象
            using (bitmap bitmap = new bitmap(bounds.width, bounds.height))
            {
                // 创建绘图对象
                using (graphics g = graphics.fromimage(bitmap))
                {
                    // 将屏幕内容复制到位图中
                    g.copyfromscreen(point.empty, point.empty, bounds.size);
                }
                this.show();
                //显示图像
                frmscreenshoteditor frmscreenshoteditor = new frmscreenshoteditor();
                frmscreenshoteditor.image = bitmap;
                frmscreenshoteditor.showdialog();
            }
        }
        catch (exception ex)
        {
            messagebox.show($"截图失败: {ex.message}", "错误",messageboxbuttons.ok, messageboxicon.error);
        }
    }
}

截图编辑窗体

public partial class frmscreenshoteditor :winformbase
 {
     public image image
     {
         get => uvcanvas.image;
         set
         {
             uvcanvas.image = value;
             invalidate();
         }
     }
     public frmscreenshoteditor()
     {
         initializecomponent();
         this.centertoparent();
         this.windowstate = formwindowstate.maximized;
     }
     private void btn_save_click(object sender, system.eventargs e)
     {
         savefiledialog  savefiledialog = new savefiledialog();
         savefiledialog.filename = "截图1";     //设置初始文件名
         savefiledialog.filter= "png image|*.png|jpeg image|*.jpg|bmp image|*.bmp";
         if (savefiledialog.showdialog()== dialogresult.ok)
         {
             string extension = path.getextension(savefiledialog.filename).tolower();
             imageformat imageformat;
             switch (extension)
             {
                 case ".png":
                     imageformat = imageformat.png;
                     break;
                 case ".jpg":
                     imageformat = imageformat.jpeg;
                     break;
                 case ".bmp":
                     imageformat = imageformat.bmp;
                     break;
                 default:
                     imageformat = imageformat.png;
                     break;
             }
             try
             {
                 image.save(savefiledialog.filename, imageformat);
                 messagebox.show($"图片已成功保存至: {savefiledialog.filename}");
             }
             catch (exception ex)
             {
                 messagebox.show($"保存图片时出错: {ex.message}");
             }
         }
     }
 }

总结

最后

以上就是基于c#实现windows桌面截图功能的详细内容,更多关于c# windows桌面截图的资料请关注代码网其它相关文章!

(0)

您想发表意见!!点此发布评论

推荐阅读

利用C#实现Window系统桌面锁定效果

05-12

C#窗体中Control以及Invalidate,Update,Refresh三种重绘方法的区别及说明

05-14

C#中的WPF基本概念详解

05-14

通过C#获取Excel单元格的数据类型的方法详解

05-11

C#特性(Attributes)和反射(Reflection)详解

05-14

C# log4net 的配置文件配置项详细介绍(配置示例)

05-09

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论