温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

获取MDI窗体中控件坐标的方法/屏幕局部截图原位写入

发布时间:2020-08-15 04:47:18 来源:网络 阅读:969 作者:fengyp 栏目:编程语言

一、程序介绍:
在有MDI窗体的工具条和菜单栏,并且窗体在任何大小时,要想准确获得窗体中控件的坐标,可以使用下列代码:

这段代码的大体含义是在原来的窗体上的pbImg(pictureBox)位置进行屏幕截图后写回pbImg,而屏幕任何内容的位置不能变化,让人看不出是截图后重新写入的。
Bitmap CatchBmp = new Bitmap(pbImg.Width, pbImg.Height);
Graphics g = Graphics.FromImage(CatchBmp);
var screenPoint = PointToScreen(pbImg.Location);
//
//x1 = panel1.Width+panel1.Left;
//y1 = menuStrip1.Height+menuStrip1.Location.Y ;
g.CopyFromScreen(new Point(screenPoint.X + panel1.Width + panel1.Left, screenPoint.Y + menuStrip1.Height + menuStrip1.Location.Y), new Point(0, 0), new Size(pbImg.Width, pbImg.Height));

二、程序中用到的典型方法介绍

1、Control.PointToScreen 方法

将指定工作区点的位置计算成屏幕坐标。
命名空间: System.Windows.Forms
程序集: System.Windows.Forms(在 system.windows.forms.dll 中)

C#语法:
public Point PointToScreen ( Point p)
参数
p:要转换的工作区坐标 Point。

返回值:一个 Point,它表示转换后的 Point、p(以屏幕坐标表示)。

2、Graphics.CopyFromScreen 方法 (Int32, Int32, Int32, Int32, Size)

执行颜色数据(对应于由像素组成的矩形)从屏幕到 Graphics 的绘图图面的位块传输。

命名空间: System.Drawing
程序集: System.Drawing(位于 System.Drawing.dll)

C#语法:
public void CopyFromScreen( int sourceX, int sourceY, int destinationX, int destinationY, Size blockRegionSize)

参数
sourceX
Type: System.Int32
位于源矩形左上角的点的 x 坐标。

sourceY
Type: System.Int32:位于源矩形左上角的点的 y 坐标。

destinationX
Type: System.Int32:位于目标矩形左上角的点的 x 坐标。

destinationY
Type: System.Int32:位于目标矩形左上角的点的 y 坐标。

blockRegionSize
Type: System.Drawing.Size:要传输的区域大小。

异常:
Win32Exception:操作失败。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI