当前位置: 编程技术>.net/c#/asp.net
c#指定区域屏幕截屏功能实现代码
来源: 互联网 发布时间:2014-08-30
本文导语: 如何不使用系统API通过纯c#实现屏幕指定区域截屏功能,截屏后还可以保存图象文件? 请参考代码: 代码示例: //截取全屏图象 private void btnFullScreen_Click(object sender, EventArgs e) { //创建图象,保存将来截取的图象 ...
如何不使用系统API通过纯c#实现屏幕指定区域截屏功能,截屏后还可以保存图象文件?
请参考代码:
代码示例:
//截取全屏图象
private void btnFullScreen_Click(object sender, EventArgs e)
{
//创建图象,保存将来截取的图象
Bitmap image = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics imgGraphics = Graphics.FromImage(image);
//设置截屏区域
imgGraphics.CopyFromScreen(0, 0, 0, 0, new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
//保存
SaveImage(image);
}
//保存图象文件
private void SaveImage(Image image)
{
if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
{
string fileName = saveFileDialog.FileName;
string extension = Path.GetExtension(fileName);
if (extension == ".jpg")
{
image.Save(fileName, ImageFormat.Jpeg);
}
else
{
image.Save(fileName, ImageFormat.Bmp);
}
}
}
private void btnFullScreen_Click(object sender, EventArgs e)
{
//创建图象,保存将来截取的图象
Bitmap image = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics imgGraphics = Graphics.FromImage(image);
//设置截屏区域
imgGraphics.CopyFromScreen(0, 0, 0, 0, new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
//保存
SaveImage(image);
}
//保存图象文件
private void SaveImage(Image image)
{
if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
{
string fileName = saveFileDialog.FileName;
string extension = Path.GetExtension(fileName);
if (extension == ".jpg")
{
image.Save(fileName, ImageFormat.Jpeg);
}
else
{
image.Save(fileName, ImageFormat.Bmp);
}
}
}