Winform开发遇到的问题之截屏功能
程序员文章站
2022-07-14 10:05:19
...
1.代码块:
//定义图片
public static Bitmap image { get; set; }
/// <summary>
/// 截图按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnShot_Click(object sender, EventArgs e)
{
//控制截图大小
Bitmap bit = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(bit);
//对整个窗体进行截图
g.CopyFromScreen(this.Location, new Point(0, 0), bit.Size);
AreaShow.image = bit;
btnSavePic_Click(null, null);
}
/// <summary>
/// 截图保存操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSavePic_Click(object sender, EventArgs e)
{
bool isSave = true;
SaveFileDialog saveImageDialog = new SaveFileDialog();
saveImageDialog.Title = "保存图片!";
saveImageDialog.Filter = @"jpeg|*.jpg|bmp|*.bmp|gif|*.gif";
if (saveImageDialog.ShowDialog() == DialogResult.OK)
{
string fileName = saveImageDialog.FileName.ToString();
if (fileName != "" && fileName != null)
{
string fileExtName = fileName.Substring(fileName.LastIndexOf(".") + 1).ToString();
System.Drawing.Imaging.ImageFormat imgformat = null;
if (fileExtName != "")
{
switch (fileExtName)
{
case "jpg":
imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
case "bmp":
imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
break;
case "gif":
imgformat = System.Drawing.Imaging.ImageFormat.Gif;
break;
default:
MessageBox.Show("只能存取为: jpg,bmp,gif 格式");
isSave = false;
break;
}
}
//默认保存为JPG格式
if (imgformat == null)
{
imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
}
if (isSave)
{
try
{
AreaShow.image.Save(fileName, imgformat);
MessageBox.Show("图片成功保存!~~");
}
catch
{
MessageBox.Show("保存失败!");
}
}
}
}
}
2.效果图:
推荐阅读
-
在iOS开发的Quartz2D使用中实现图片剪切和截屏功能
-
Mobile Web开发基础之四--处理手机设备的横竖屏问题
-
Winform开发遇到的问题之截屏功能
-
Winform开发框架之参数配置管理功能实现-基于SettingsProvider.net的构建
-
WIFI模块的常用功能开发和遇到的问题,以及解决方法
-
在iOS开发的Quartz2D使用中实现图片剪切和截屏功能
-
Mobile Web开发基础之四--处理手机设备的横竖屏问题
-
PHP微信开发之微信消息自动回复遇到的问题
-
Winform开发框架之参数配置管理功能实现-基于SettingsProvider.net的构建
-
PHP微信开发之微信消息自动回复遇到的问题