我有一个Winforms应用程序,用户可以使用它来进行区域截屏。我想添加一个小预览窗口,但是不确定如何实现。到目前为止,我尝试在鼠标移动时重新创建位图,但是这样做太卡了,无法使用。因此,我想如果我使用预定义的图像(整个屏幕的截图),并根据鼠标位置将其移动到Picturebox中,以便您可以放大查看屏幕(更轻松地选择要截取的精确像素)。我不确定如何实现这一点,我对绘图也很陌生,下面是我现在的代码。
private void falseDesktop_MouseMove(object sender, MouseEventArgs e)
{
zoomBox.Image = showZoomBox(e.Location);
zoomBox.Invalidate();
}
private Image showZoomBox(Point curLocation)
{
int x = 0;
int y = 0;
if (curLocation.X - 12 <= 0)
{
x = curLocation.X - 12;
}
else
{
x = curLocation.X;
}
if (curLocation.Y - 11 <= 0)
{
y = curLocation.Y - 11;
}
else
{
y = curLocation.Y;
}
Point start = new Point(curLocation.X - 12, curLocation.Y - 11);
Size size = new Size(24, 22);
Rectangle rect = new Rectangle(start, size);
Image selection = cropImage(falseDesktop.Image, rect);
return selection;
}
private static Image cropImage(Image img, Rectangle cropArea)
{
if (cropArea.Width != 0 && cropArea.Height != 0)
{
Bitmap bmpImage = new Bitmap(img);
Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
bmpImage.Dispose();
return (Image)(bmpCrop);
}
return null;
}
编辑:
这是一个模拟图,如您所请求的:
这张图片黑色的部分是一个面板,其中文本是标签,您看到的图像区域(堆栈溢出)将是图片框(称为zoomBox),放在zoomBox上方的线条将成为指南,两条线相交的位置将是鼠标的位置。希望这可以更好地帮助您理解我的问题。
另一件可能有助于解释我的问题的事情是,该窗体实际上填充了整个屏幕,带有“假桌面”。它是一个图片框,覆盖整个屏幕,截取按下“打印屏幕”时桌面的屏幕截图。所以我希望这个小的“预览窗格”基本上是放大了鼠标所在位置的位置。