C# 截取指点区域的屏幕,并存为文件,同时在窗口中加载预览

本文介绍了一个简单的屏幕截图工具实现方法,使用C#编程语言通过Windows Forms应用程序捕获屏幕区域为图片,并提供了保存和预览的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果如图:
C 截取指点区域的屏幕,并存为文件,同时在窗口中加载预览 - 加菲 - 面包会有的
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;



namespace CopyFromScreen
{
public partial class Form1 : Form
{


public Form1()
{
InitializeComponent();


}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
int width=0;//宽
int height = 0;//高
int x = 0;//起始点x坐标
int y = 0;//起始点y坐标

bool result1 = int.TryParse(this.textBox3.Text,out width);//从文本框获取值
bool result2 = int.TryParse(this.textBox4.Text,out height);
bool result3 = int.TryParse(this.textBox1.Text,out x);
bool result4 = int.TryParse(this.textBox2.Text,out y);

string ScreenshotPath = this.textBox5.Text;//存储路径

try
{
Image image = new Bitmap(width, height);
Graphics g = Graphics.FromImage(image);

g.CopyFromScreen(x, y, 0, 0, new System.Drawing.Size(width, height));


string hour = DateTime.Now.Minute.ToString();
string second = DateTime.Now.Second.ToString();
string filepath = ScreenshotPath + "\\" + hour + "_" + second + ".jpg";//生成完整路径
image.Save(filepath);

this.pictureBox1.Image = Image.FromFile(@filepath);//显示到控件预览

}
catch
{

}
}

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("iexplore.exe", "http://wmnmtm.blog.163.com");
}

private void pictureBox1_Click(object sender, EventArgs e)
{

}
}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值