C# 捕捉屏幕并没有捕捉到正确的区域
在C#中,我编写了一个应用程序,在该应用程序中,您可以选择一个要拍摄屏幕快照的框,如果您在X轴上的0以上,它就不会拍摄照片。我画了一个小图片来说明我的意思: 红色=图像实际上不属于该部分 黑色=正常 我的代码如下:C# 捕捉屏幕并没有捕捉到正确的区域,c#,winforms,screenshot,C#,Winforms,Screenshot,在C#中,我编写了一个应用程序,在该应用程序中,您可以选择一个要拍摄屏幕快照的框,如果您在X轴上的0以上,它就不会拍摄照片。我画了一个小图片来说明我的意思: 红色=图像实际上不属于该部分 黑色=正常 我的代码如下: #region testing private Point start = Point.Empty; private Point end = Point.Empty; private void Form2_MouseDown(object sender, MouseEventA
#region testing
private Point start = Point.Empty;
private Point end = Point.Empty;
private void Form2_MouseDown(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) != 0)
{
start.X = e.X;
start.Y = e.Y;
}
}
private void Form2_MouseMove(object sender, MouseEventArgs e)
{
Point p1;
Point p2;
if (((e.Button & MouseButtons.Left) != 0) && (start != Point.Empty))
{
using (Graphics g = this.CreateGraphics())
{
p1 = PointToScreen(start);
if (end != Point.Empty)
{
p2 = PointToScreen(end);
ControlPaint.DrawReversibleFrame(GetRectangleForPoints(p1, p2),
Color.Black, FrameStyle.Dashed);
}
end.X = e.X;
end.Y = e.Y;
p2 = PointToScreen(end);
ControlPaint.DrawReversibleFrame(GetRectangleForPoints(p1, p2),
Color.Black, FrameStyle.Dashed);
}
}
}
private void Form2_MouseUp(object sender, MouseEventArgs e)
{
Point p1;
Point p2;
if ((end != Point.Empty) && (start != Point.Empty))
{
using (Graphics g = this.CreateGraphics())
{
p1 = PointToScreen(start);
p2 = PointToScreen(end);
ControlPaint.DrawReversibleFrame(GetRectangleForPoints(p1, p2),
Color.Black, FrameStyle.Dashed);
int x1 = p1.X;
int y1 = p1.Y;
int x2 = p2.X;
int y2 = p2.Y;
int x = x2 - x1;
int y = y2 - y1;
string[] xsp;
int rx = 0;
string[] ysp;
int ry = 0;
if (x.ToString().Contains("-"))
{
xsp = x.ToString().Split('-');
rx = Convert.ToInt32(xsp[1]);
}
else
{
rx = x;
}
if (y.ToString().Contains("-"))
{
ysp = y.ToString().Split('-');
ry = Convert.ToInt32(ysp[1]);
}
else
{
ry = y;
}
using (Bitmap bmpScreenCapture = new Bitmap(rx, ry, g))
{
using (Graphics gra = Graphics.FromImage(bmpScreenCapture))
{
if(x.ToString().Contains("-"))
{
gra.CopyFromScreen(x2, y1, 0, 0, bmpScreenCapture.Size,
CopyPixelOperation.SourceCopy);
}
else if(!x.ToString().Contains("-"))
{
gra.CopyFromScreen(x1, y1, 0, 0, bmpScreenCapture.Size,
CopyPixelOperation.SourceCopy);
}
else if(y.ToString().Contains("-"))
{
gra.CopyFromScreen(x1, y2, 0, 0, bmpScreenCapture.Size,
CopyPixelOperation.SourceCopy);
}
else if (!y.ToString().Contains("-"))
{
gra.CopyFromScreen(x1, y1, 0, 0, bmpScreenCapture.Size,
CopyPixelOperation.SourceCopy);
}
else if (x.ToString().Contains("-") && y.ToString().Contains("-"))
{
gra.CopyFromScreen(x2, y2, 0, 0, bmpScreenCapture.Size,
CopyPixelOperation.SourceCopy);
}
string filename = GenerateRandomString(20) + ".png";
bmpScreenCapture.Save(Path.GetTempPath() + "" + filename,
ImageFormat.Png);
ControlPaint.DrawReversibleFrame(GetRectangleForPoints
(new Point(0), new Point(0)), Color.Black, FrameStyle.Dashed);
//Upload(Path.GetTempPath() + "" + filename, filename);
}
}
}
}
start = Point.Empty;
end = Point.Empty;
}
private Rectangle GetRectangleForPoints(Point beginPoint, Point endPoint)
{
int top = beginPoint.Y < endPoint.Y ? beginPoint.Y : endPoint.Y;
int bottom = beginPoint.Y > endPoint.Y ? beginPoint.Y : endPoint.Y;
int left = beginPoint.X < endPoint.X ? beginPoint.X : endPoint.X;
int right = beginPoint.X > endPoint.X ? beginPoint.X : endPoint.X;
rect = new Rectangle(left, top, (right - left), (bottom - top));
return rect;
}
#endregion
#区域测试
私有点开始=点。空;
私有点结束=点。空;
私有void Form2_MouseDown(对象发送方,MouseEventArgs e)
{
如果((e.按钮和鼠标按钮。左)!=0)
{
start.X=e.X;
start.Y=e.Y;
}
}
私有void Form2\u MouseMove(对象发送方,MouseEventArgs e)
{
p1点;
p2点;
如果(((e.按钮和鼠标按钮.左)!=0)和&(开始!=点.空))
{
使用(Graphics g=this.CreateGraphics())
{
p1=指向屏幕(开始)的点;
如果(结束!=点为空)
{
p2=指向屏幕的点(结束);
ControlPaint.DrawReversibleFrame(获取点(p1,p2)的矩形),
颜色。黑色,框架样式。虚线);
}
end.X=e.X;
end.Y=e.Y;
p2=指向屏幕的点(结束);
ControlPaint.DrawReversibleFrame(获取点(p1,p2)的矩形),
颜色。黑色,框架样式。虚线);
}
}
}
专用作废表单2_MouseUp(对象发送方,MouseEventArgs e)
{
p1点;
p2点;
如果((结束!=点.空)和&(开始!=点.空))
{
使用(Graphics g=this.CreateGraphics())
{
p1=指向屏幕(开始)的点;
p2=指向屏幕的点(结束);
ControlPaint.DrawReversibleFrame(获取点(p1,p2)的矩形),
颜色。黑色,框架样式。虚线);
int-x1=p1.X;
int y1=p1.Y;
intx2=p2.X;
int y2=p2.Y;
int x=x2-x1;
int y=y2-y1;
字符串[]xsp;
int rx=0;
字符串[]ysp;
int-ry=0;
如果(x.ToString()包含(“-”)
{
xsp=x.ToString().Split('-');
rx=转换为32(xsp[1]);
}
其他的
{
rx=x;
}
如果(y.ToString()包含(“-”)
{
ysp=y.ToString().Split('-');
ry=转换为32(ysp[1]);
}
其他的
{
y=y;
}
使用(位图bmpScreenCapture=新位图(rx、ry、g))
{
使用(Graphics gra=Graphics.FromImage(bmpScreenCapture))
{
如果(x.ToString()包含(“-”)
{
gra.CopyFromScreen(x2,y1,0,0,BMP屏幕尺寸,
CopyPixelOperation.SourceCopy);
}
如果(!x.ToString()包含(“-”),则为else
{
从屏幕(x1,y1,0,0,BMP屏幕尺寸,
CopyPixelOperation.SourceCopy);
}
如果(y.ToString()包含(“-”),则为else
{
从屏幕(x1,y2,0,0,BMP屏幕尺寸,
CopyPixelOperation.SourceCopy);
}
如果(!y.ToString()包含(“-”),则为else
{
从屏幕(x1,y1,0,0,BMP屏幕尺寸,
CopyPixelOperation.SourceCopy);
}
如果(x.ToString()包含(“-”)和&y.ToString()包含(“-”)
{
gra.CopyFromScreen(x2,y2,0,0,BMP屏幕尺寸,
CopyPixelOperation.SourceCopy);
}
字符串文件名=GeneratorDomainString(20)+“.png”;
bmpScreenCapture.Save(Path.GetTempPath()+“”+文件名,
ImageFormat.Png);
ControlPaint.DrawReversibleFrame(GetRectangleForPoints
(新点(0),新点(0)),颜色。黑色,框架样式。虚线);
//上载(Path.GetTempPath()+“”+文件名,文件名);
}
}
}
}
开始=点。空;
结束=点。空;
}
私有矩形GetRectangleForPoints(点起点、点终点)
{
int top=beginPoint.YendPoint.Y?beginPoint.Y:endPoint.Y;
int left=beginPoint.XendPoint.X?beginPoint.X:endPoint.X;
rect=新矩形(左、上、(右-左)、(下-上));
返回矩形;
}
#端区
我试图纠正它,但没有成功。我的意思是,图片仍然显示,但它不是屏幕的正确区域。我开始使用不同的选择方法,这允许我获得矩形X和Y以及大小。更好的解决方案:) 代码:
#区域测试
私有点开始=点。空;
私有点结束=点。空;
私有void Form2_MouseDown(对象发送方,MouseEventArgs e)
{
如果((e.按钮和鼠标按钮。左)!=0)
{
start.X=e.X;
start.Y=e.Y;
}
}
私有void Form2\u MouseMove(对象发送方,MouseEventArgs e)
{
//这个。使无效();
p1点;
p2点;
如果(((e.按钮和鼠标按钮.左)!=0)和&(开始!=点.空))
{
使用(Graphics g=this.CreateGraphics())
{
g、 清晰(此为背景色);
p1=指向屏幕(开始)的点;
如果(结束!=点为空)
{
p2=指向屏幕
#region testing
private Point start = Point.Empty;
private Point end = Point.Empty;
private void Form2_MouseDown(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) != 0)
{
start.X = e.X;
start.Y = e.Y;
}
}
private void Form2_MouseMove(object sender, MouseEventArgs e)
{
//this.Invalidate();
Point p1;
Point p2;
if (((e.Button & MouseButtons.Left) != 0) && (start != Point.Empty))
{
using (Graphics g = this.CreateGraphics())
{
g.Clear(this.BackColor);
p1 = PointToScreen(start);
if (end != Point.Empty)
{
p2 = PointToScreen(end);
ControlPaint.DrawReversibleFrame(GetRectangleForPoints(p1, p2), Color.Black, FrameStyle.Dashed);
}
end.X = e.X;
end.Y = e.Y;
p2 = PointToScreen(end);
ControlPaint.DrawReversibleFrame(GetRectangleForPoints(p1, p2), Color.Black, FrameStyle.Dashed);
}
}
}
private void Form2_MouseUp(object sender, MouseEventArgs e)
{
Point p1;
Point p2;
if ((end != Point.Empty) && (start != Point.Empty))
{
using (Graphics g = this.CreateGraphics())
{
p1 = PointToScreen(start);
p2 = PointToScreen(end);
ControlPaint.DrawReversibleFrame(GetRectangleForPoints(p1, p2), Color.Black, FrameStyle.Dashed);
int x1 = p1.X;
int y1 = p1.Y;
int x2 = p2.X;
int y2 = p2.Y;
int x = x2 - x1;
int y = y2 - y1;
string[] xsp;
int rx = 0;
string[] ysp;
int ry = 0;
if (x.ToString().Contains("-"))
{
xsp = x.ToString().Split('-');
rx = Convert.ToInt32(xsp[1]);
}
else
{
rx = x;
}
if (y.ToString().Contains("-"))
{
ysp = y.ToString().Split('-');
ry = Convert.ToInt32(ysp[1]);
}
else
{
ry = y;
}
using (Bitmap bmpScreenCapture = new Bitmap(rect.Width, rect.Height, g))
{
using (Graphics gra = Graphics.FromImage(bmpScreenCapture))
{
gra.CopyFromScreen(rect.X, rect.Y, 0, 0, bmpScreenCapture.Size, CopyPixelOperation.SourceCopy);
string filename = GenerateRandomString(20) + ".png";
bmpScreenCapture.Save(Path.GetTempPath() + "" + filename, ImageFormat.Png);
g.Clear(this.BackColor);
//Upload(Path.GetTempPath() + "" + filename, filename);
}
}
}
}
start = Point.Empty;
end = Point.Empty;
}
private Rectangle GetRectangleForPoints(Point beginPoint, Point endPoint)
{
int top = beginPoint.Y < endPoint.Y ? beginPoint.Y : endPoint.Y;
int bottom = beginPoint.Y > endPoint.Y ? beginPoint.Y : endPoint.Y;
int left = beginPoint.X < endPoint.X ? beginPoint.X : endPoint.X;
int right = beginPoint.X > endPoint.X ? beginPoint.X : endPoint.X;
rect = new Rectangle(left, top, (right - left), (bottom - top));
return rect;
}
#endregion