32人参与 • 2025-05-12 • Asp.net
核心方法1: screen.getbounds(point)
核心方法2: copyfromscreen(point,point,point)
自定义显示图像窗体: frmscreenshoteditor
控件代码在文末链接,需要者自行下载。
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桌面截图的资料请关注代码网其它相关文章!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论