我自己写了一个winform的web浏览器,想实现截图功能,就像是这样:
截图的时候点击截图键就可以开始了,然后选择区域,双击就可以直接保存了,就像这样。
可是问题来了,如上图,我选择的是“百度搜索”的区域,可是截下来的却是整个电脑屏幕的左上角那一部分。如图:
自己看了很久都没有找出问题,求哪位大神来帮帮我!
二楼放截图代码。。。。
以上就是全部的截图代码,我自己总是看不出问题来~
无论选择什么区域,永远都是截左上角。。。。。。
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;
using System.Drawing.Imaging;
namespace SelfBrowser
{
public partial class Cutter : Form
{
public Cutter()
{
InitializeComponent();
}
#region 定义程序变量
// 定义变量
// 用来记录鼠标按下的坐标,用来确定绘图起点
private Point DownPoint;
// 用来表示是否截图完成
private bool CatchFinished = false;
// 用来表示截图开始
private bool CatchStart = false;
// 用来保存原始图像
private Bitmap originBmp;
// 用来保存截图的矩形
private Rectangle CatchRectangle;
#endregion
/// <summary>
/// 窗体初始化操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Cutter_Load(object sender, EventArgs e)
{
// 设置控件样式为双缓冲,这样可以有效减少图片闪烁的问题
// 第二个参数为true表示把第一个参数指定的样式应用于控件;false 表示不应用。
// '|'表示位逻辑或运算
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
this.UpdateStyles();
// 改变鼠标样式
this.Cursor = Cursors.Cross;
// 保存全屏图片
originBmp = new Bitmap(this.BackgroundImage);
}
/// <summary>
/// 右键点击退出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Cutter_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
}
/// <summary>
/// 左键按下开始截屏
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Cutter_MouseDown(object sender, MouseEventArgs e)
{
// 鼠标左键按下是开始画图,也就是截图
if (e.Button == MouseButtons.Left)
{
// 如果捕捉没有开始
if (!CatchStart)
{
CatchStart = true;
// 保存此时鼠标按下坐标
DownPoint = new Point(e.X, e.Y);
}
}
}
/// <summary>
/// 左键双击保存到剪贴板
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
///
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // 目标 DC的句柄
int nXDest,
int nYDest,
int nWidth,
int nHeight,
IntPtr hdcSrc, // 源DC的句柄
int nXSrc,
int nYSrc,
System.Int32 dwRop // 光栅的处理数值
);
private void Cutter_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && CatchFinished)
{
// 新建一个与矩形一样大小的空白图片
Bitmap CatchedBmp = new Bitmap(CatchRectangle.Width, CatchRectangle.Height);
Graphics g = Graphics.FromImage(CatchedBmp);
// 把originBmp中指定部分按照指定大小画到空白图片上
// CatchRectangle指定originBmp中指定部分
// 第二个参数指定绘制到空白图片的位置和大小
// 画完后CatchedBmp不再是空白图片了,而是具有与截取的图片一样的内容
g.DrawImage(originBmp, new Rectangle(0, 0, CatchRectangle.Width, CatchRectangle.Height), CatchRectangle, GraphicsUnit.Pixel);
// 将图片保存到剪切板中
Clipboard.SetImage(CatchedBmp);
g.Dispose();
CatchFinished = false;
this.BackgroundImage = originBmp;
CatchedBmp.Dispose();
this.DialogResult = DialogResult.OK;
this.Close();
}
//获得当前屏幕的大小 http://www.cnblogs.com/roucheng/
Rectangle rect = new Rectangle();
rect = Screen.GetWorkingArea(this);
//创建一个以当前屏幕为模板的图象
Graphics g1 = this.CreateGraphics();
//创建以屏幕大小为标准的位图
//Bitmap CatchedBmp = new Bitmap(CatchRectangle.Width, CatchRectangle.Height);
Image MyImage = new Bitmap(CatchRectangle.Width, CatchRectangle.Height, g1);
Graphics g2 = Graphics.FromImage(MyImage);
//得到屏幕的DC
IntPtr dc1 = g1.GetHdc();
//得到Bitmap的DC
IntPtr dc2 = g2.GetHdc();
//调用此API函数,实现屏幕捕获
BitBlt(dc2, 0, 0, CatchRectangle.Width, CatchRectangle.Height, dc1, 0, 0, 13369376);
//BitBlt(dc2, 0, 0, rect.Width, rect.Height, dc1, 0, 0, 13369376);
//释放掉屏幕的DC
g1.ReleaseHdc(dc1);
//释放掉Bitmap的DC
g2.ReleaseHdc(dc2);
//以JPG文件格式来保存
MyImage.Save(@"D:/浏览器截图.jpg", ImageFormat.Jpeg);
MessageBox.Show("当前屏幕已经保存在D盘,名称:浏览器截图.jpg!");
}
/// <summary>
/// 鼠标移动事件处理程序,即用户改变截图大小的处理
/// 这个方法是截图功能的核心方法,也就是绘制截图
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Cutter_MouseMove(object sender, MouseEventArgs e)
{
// 确保截图开始
if (CatchStart)
{
// 新建一个图片对象,让它与屏幕图片相同
Bitmap copyBmp = (Bitmap)originBmp.Clone();
// 获取鼠标按下的坐标
Point newPoint = new Point(DownPoint.X, DownPoint.Y);
// 新建画板和画笔
Graphics g = Graphics.FromImage(copyBmp);
Pen p = new Pen(Color.Red, 1);
// 获取矩形的长宽
int width = Math.Abs(e.X - DownPoint.X);
int height = Math.Abs(e.Y - DownPoint.Y);
if (e.X < DownPoint.X)
{
newPoint.X = e.X;
}
if (e.Y < DownPoint.Y)
{
newPoint.Y = e.Y;
}
CatchRectangle = new Rectangle(newPoint, new Size(width, height));
// 将矩形画在画板上
g.DrawRectangle(p, CatchRectangle);
// 释放目前的画板
g.Dispose();
p.Dispose();
// 从当前窗体创建新的画板
Graphics g1 = this.CreateGraphics();
// 将刚才所画的图片画到截图窗体上
// 为什么不直接在当前窗体画图呢?
// 如果自己解决将矩形画在窗体上,会造成图片抖动并且有无数个矩形
// 这样实现也属于二次缓冲技术
g1.DrawImage(copyBmp, new Point(0, 0));
g1.Dispose();
// 释放拷贝图片,防止内存被大量消耗
copyBmp.Dispose();
}
}
/// <summary>
/// 左键弹起结束截屏
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Cutter_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// 如果截图已经开始,鼠标左键弹起设置截图完成
if (CatchStart)
{
CatchStart = false;
CatchFinished = true;
}
}
}
}
}
如果可以的话,能帮我把错误改正过来吗?心好累啊。。。
无论选择什么区域,永远都是截左上角。。
是坐标相对位置的问题,偏移量