2013-08-22 57 views
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; 
} 

的意见,我被给出如下:

You're looking at the value of OnUpdateStatus within the CaptureImage method, right? So it matters which instead of ScreenCapture you call the method on. I suspect you need to pass _screenCap to the constructor of Form1 (which would need to store it in a field) so that you could use the same instance when you call CaptureImage within Form1

我不知道如何实施给我的建议。在我的代码前两行,我只是想拿走我的抓屏类的新实例的创建和写入

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

但这生成以下错误:

Error 1 An object reference is required for the non-static field, method, or property 'DotFlickScreenCapture.ScreenCapture.CaptureImage(bool, System.Drawing.Size, System.Drawing.Point, System.Drawing.Point, System.Drawing.Point, System.Drawing.Rectangle, string, string)'

因此,要摆脱这个错误我把方法被称为静态类,但这产生不同的错误一大堆我的代码尝试存储拍摄的图像:

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

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

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

是否有人能够帮助我的图像显示在图像框中?

回答

0

我真的不明白你的代码。但我明白这个建议是给你的。它表示将捕获屏幕的对象传递给表单的构造函数:

可以说你有一个表单名称form1。这里是构造和少的代码必须具有在form1类:

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" 
    } 
} 

构造正在捕捉的图像作为对象(图像imgObj),并将其分配给字段(图像CapturedImage)。

如果要在picturebox中显示它。只需在构造函数中加入这一行,以及:

picturebox.Image = CapturedImage; 

我们从另一种形式叫,像这样做:

捕获屏幕,你正在做的(你的第一个代码是正确的,在其中创建ScreenCapture类的对象),并将其保存在一个对象:

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

现在创建的form1实例,并捕获图像传递给形式的构造:

form1 ImageForm = new form1(CapturedImageObj); 

并显示它:

form1.Show(); 
相关问题