2013-08-22 59 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(); 
相關問題