屏幕截图带有网格到其他Windows窗体的图片框 [英] Screenshot Picturebox with Grid to other Windows Form
问题描述
大家好,
有没有办法截取第三个图片框?
is there a way to take screenshot of the 3rd picturebox?
我想将彩色网格复制到另一个窗体
I'd like to copy the colored grid to another windows form
推荐答案
您好,
以下
GitHub存储库有一个类来处理截图并返回位图。使用PictureBox的句柄调用Capture并将其传递给Capture。请注意下面的代码我将Capture的修饰符从private更改为public。将
类的命名空间更改为您的命名空间。
The following GitHub repository has a class to handle taking screenshots and return a bitmap. Call Capture with the handle of PictureBox and pass it to Capture. Note in the code below I changed the modifier of Capture from private to public. Change the namespace of the class to your namespace.
using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace BaseLibrary.SupportClasses
{
public class ScreenCapture
{
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetDesktopWindow();
[StructLayout(LayoutKind.Sequential)]
private struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll")]
private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
public static Image CaptureFullscreen()
{
return Capture(GetDesktopWindow());
}
public static Bitmap CaptureCurrentWindow()
{
return Capture(GetForegroundWindow());
}
public static Bitmap Capture(IntPtr handle)
{
var rect = new Rect();
GetWindowRect(handle, ref rect);
var bounds = new Rectangle(
rect.Left, rect.Top, rect.Right - rect.Left,
rect.Bottom - rect.Top);
var result = new Bitmap(bounds.Width, bounds.Height);
using (var graphics = Graphics.FromImage(result))
{
graphics.CopyFromScreen(
new Point(bounds.Left, bounds.Top),
Point.Empty, bounds.Size);
}
return result;
}
}
}
调用它
var imageBitmap = ScreenCapture.Capture(pictureBox1.Handle);
对于另一个表单,创建一个重载的构造函数,它将Bitmap传递给私有变量,然后在表单加载时分配BitMap到PictureBox。
For the other form, create a overloaded constructor which passes the Bitmap in to a private variable then on form load assign the BitMap to the PictureBox.
这篇关于屏幕截图带有网格到其他Windows窗体的图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!