[英]Selection of the screen shot makes program crash C#
i have a program which takes a screen shot of the selected area (which I select with a mouse) and saves it to a clipboard. 我有一个程序,可以对选定区域(用鼠标选择)进行截屏,然后将其保存到剪贴板。 The problem is it works only if i make a selection from top to bottom.
问题是,只有当我从上到下进行选择时,它才起作用。 If I try to make a selection in any other direction (bottom to top, right to left, left to right) the program crashes.
如果我尝试在其他任何方向(从下到上,从右到左,从左到右)进行选择,则程序将崩溃。 This is the code for MouseMove:
这是MouseMove的代码:
public void Window_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (this.isMouseDown)
{
double curx = e.GetPosition(null).X;
double cury = e.GetPosition(null).Y;
System.Windows.Shapes.Rectangle r = new System.Windows.Shapes.Rectangle();
SolidColorBrush brush = new SolidColorBrush(Colors.White);
r.Stroke = brush;
r.Fill = brush;
r.StrokeThickness = 1;
r.Width = Math.Abs(curx - x);
r.Height = Math.Abs(cury - y);
selekt.Children.Clear();
selekt.Children.Add(r);
Canvas.SetLeft(r, x);
Canvas.SetTop(r, y);
if (e.LeftButton == MouseButtonState.Released)
{
selekt.Children.Clear();
width = e.GetPosition(null).X - x;
height = e.GetPosition(null).Y - y;
this.CaptureScreen(x, y, width, height);
this.x = this.y = 0;
this.isMouseDown = false;
this.Close();
}
}
}
And this is for CaptureScreen: 这是用于CaptureScreen的:
public void CaptureScreen(double x, double y, double width, double height)
{
int ix, iy, iw, ih;
ix = Convert.ToInt32(x);
iy = Convert.ToInt32(y);
iw = Convert.ToInt32(width);
ih = Convert.ToInt32(height);
Bitmap slika = new Bitmap(iw, ih, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(slika);
g.CopyFromScreen(ix, iy, 0, 0,new System.Drawing.Size(iw, ih),CopyPixelOperation.SourceCopy);
System.Windows.Forms.Clipboard.SetImage(slika);
are you getting error the below mentioned code? 您在以下提到的代码中出错吗? That seems to be the place where you would get one.
那似乎是您可以得到的地方。
Canvas.SetLeft(r, x);
Canvas.SetTop(r, y);
If yes, then thats because SetLeft takes UIElement and a double values 如果是,那是因为SetLeft采用了UIElement和一个double值
public static void SetLeft(
UIElement element,
double length)
And also, I am guessing that x and y are double and declared public. 而且,我猜想x和y是双精度的,并声明为public。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.