0

我试图让我的屏幕截图图像显示在我的控制面板表单上的图片框中。但是图像并没有被忽略。

我被告知问题可能出在以下代码行中:

      ScreenCapture capture = new ScreenCapture();
         capture.CaptureImage(showCursor, curSize, curPosition, startPoint, Point.Empty, bounds, _screenPath, fi);

当我创建一个新的屏幕截图时,数据没有传递到我的图片框。当我运行我的程序时,错误源于以下行总是返回 null:

 Image img = (Image)bitmap;
 if (OnUpdateStatus == null) return;

 ProgressEventArgs args = new ProgressEventArgs(img);
 OnUpdateStatus(this, args);

然后在我的控制面板winform中,我尝试按如下方式显示图像:

private ScreenCapture _screenCap;

public ControlPanel()
{
    InitializeComponent();
    _screenCap = new ScreenCapture();
    _screenCap.OnUpdateStatus += _screen_CapOnUpdateStatus;

}



private void _screen_CapOnUpdateStatus(object sender, ProgressEventArgs e)
{

    imagePreview.Image = e.CapturedImage;
}

我得到的建议如下:

您正在查看 CaptureImage 方法中 OnUpdateStatus 的值,对吗?因此,调用该方法而不是 ScreenCapture 很重要。我怀疑您需要将 _screenCap 传递给 Form1 的构造函数(需要将其存储在字段中),以便在 Form1 中调用 CaptureImage 时可以使用相同的实例

我不知道如何实施给我的建议。在我的前两行代码中,我只是试图取消创建 ScreenCapture 类的新实例并编写

  ScreenCapture.CaptureImage(showCursor, curSize, curPosition, startPoint, Point.Empty, bounds, _screenPath, fi);

但这会产生以下错误:

错误 1 ​​非静态字段、方法或属性需要对象引用 'DotFlickScreenCapture.ScreenCapture.CaptureImage(bool, System.Drawing.Size, System.Drawing.Point, System.Drawing.Point, System.Drawing.Point , System.Drawing.Rectangle, 字符串, 字符串)'

因此,为了摆脱这个错误,我将被调用的方法转换为静态类,但这会在我的代码尝试存储拍摄的图像时产生大量不同的错误:

     Image img = (Image)bitmap;
     if (OnUpdateStatus == null) return;

     ProgressEventArgs args = new ProgressEventArgs(img);
     OnUpdateStatus(this, args);

它声称我的 OnUpdateStatus 需要一个对象引用,并且使用我拥有的 THIS 关键字在静态字段或环境中无效。

有没有人可以帮助让我的图像显示在图像框中?

4

1 回答 1

0

Sincerely I could not understand your code. But I understood the advice was given to you. It says that pass the object of captured screen to the form's constructor:

Lets say you have a form name form1. Here is the constructor and little code you must have in the form1 class:

public partial class Form1 : Form
{
    Image CapturedImage;

    public Form1(Image imgObj) //"you need to pass _screenCap to the constructor of Form1"
    {
        InitializeComponent();
        CapturedImage = imgObj; //"which would need to store it in a field"
    }
}

The constructor is taking the captured image as an object(Image imgObj) and assign it to the field(Image CapturedImage;).

If you want to display it in the picturebox. Simply add this line in the constructor as well:

picturebox.Image = CapturedImage;

Now to call it from another form, do it like this:

Capture screen as you are doing (your first code is correct in which you create object of ScreenCapture class) and save it in an object:

Image CapturedImageObj = capture.CaptureImage(showCursor, curSize, curPosition, startPoint, Point.Empty, bounds, _screenPath, fi);

Now create instance of form1 and pass captured image to the constructor of the form:

form1 ImageForm = new form1(CapturedImageObj);

and display it:

form1.Show();
于 2013-08-22T16:45:06.773 回答