2013-08-23 50 views
-1

我遇到了轉換錯誤我想要克服一些幫助。將void類型轉換爲System.Drawing.Image

目前我正在嘗試拍攝我的桌面並將其存儲在一個變量中,我可以通過它。現在,該代碼如下所示:

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

但是,這樣做,我得到以下錯誤:

Cannot implicitly convert type 'void' to 'System.Drawing.Image'

所以,我想類型轉換的capture.CaptureImage和它產生的相同錯誤。我寫的路線是這樣的:

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

我CaptureImage方法如下:

public void CaptureImage(bool showCursor, Size curSize, Point curPos, Point SourcePoint, Point DestinationPoint, Rectangle SelectionRectangle, string FilePath, string extension) 
    { 
     Bitmap bitmap = new Bitmap(SelectionRectangle.Width, SelectionRectangle.Height); 

     using (Graphics g = Graphics.FromImage(bitmap)) 
     { 
      g.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size); 

      if (showCursor) 
      { 
       Rectangle cursorBounds = new Rectangle(curPos, curSize); 
       Cursors.Default.Draw(g, cursorBounds); 
      } 
     } 

     if (saveToClipboard) 
     { 
      Image img = (Image)bitmap; 

      if (OnUpdateStatus == null) return; 

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

     } 

所以,看到我已經設置爲無效的方法,我改變它,以便它需要一個圖像像這樣:

public Image CaptureImage(bool showCursor, Size curSize, Point curPos, Point SourcePoint, Point DestinationPoint, Rectangle SelectionRectangle, string FilePath, string extension) 

但隨後生成以下錯誤:

An object of a type convertible to 'System.Drawing.Image' is required.

此錯誤指向下面的代碼塊與第二if語句是罪魁禍首:

if (saveToClipboard) 
    { 
    Image img = (Image)bitmap; 

    if (OnUpdateStatus == null) return; 

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

我已經跑出來的關於如何擊敗這些錯誤的想法。任何人都可以擺脫一些輕/新的見解,以便我可以擺脫它們?

+1

爲了將來的參考,儘管通常很好地展示你嘗試過的東西。在這個例子中,通過顯示代碼的每個變化,使問題變得有點混亂。最好是隻顯示你當前的代碼,錯誤信息和錯誤信息......然後,Google錯誤消息應該已經解決了你的問題 – musefan

+0

只是想給我一個詳細的概述嘗試並避免投票。沒有工作:( – N0xus

+0

這不是我的失望,我知道你只是儘量提供儘可能多的信息。只是在這種情況下,你可能已經有太多的代碼變化讓一些東西感到困惑 – musefan

回答

3

您的CaptureImage需要返回,但您在if (OnUpdateStatus == null)中返回void/nothing。即使你在那裏還有一些圖像,你仍然必須將該圖像返回到if塊以外,否則可能會抱怨Not all code paths return a value

public Image CaptureImage(bool showCursor, Size curSize, Point curPos, Point SourcePoint, Point DestinationPoint, Rectangle SelectionRectangle, string FilePath, string extension) 
{ 
    Bitmap bitmap = new Bitmap(SelectionRectangle.Width, SelectionRectangle.Height); 

    using (Graphics g = Graphics.FromImage(bitmap)) 
    { 
     g.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size); 

     if (showCursor) 
     { 
      Rectangle cursorBounds = new Rectangle(curPos, curSize); 
      Cursors.Default.Draw(g, cursorBounds); 
     } 
    } 

    if (saveToClipboard) 
    { 
     Image img = (Image)bitmap; 

     if (OnUpdateStatus == null) return bitmap;//<--- here 

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

    } 
    return bitmap;//<--- and here 
} 
1

,因爲你的方法

public Image CaptureImage( 

預計,不必返回圖像的返回類型。代碼中有2個回報點。

點1:

if (OnUpdateStatus == null) return; 

給出錯誤,因爲作爲返回類型必須是形象,你不回來,好讓你們應該做這樣

if (OnUpdateStatus == null) 
    return null; //or a valid image 

和退出你的方法之前,需要返回圖片

return someImage; //as the last line in your method before closing brackets 
+2

@musefan StackExchange不是一個自我的地方,請提高你的評論的效率,例如指出是什麼讓答案蹩腳。 – Romoku

+0

@Romoku:評論框將不會容納足夠的字符來複制和粘貼問題如果NoOne讀取這個問題是完全的,那麼他們就會知道問題是什麼。我不認爲「自我」在這方面是一個相關的指責。 – musefan

1

您需要從您的CaptureImage方法實際返回圖像。您的退貨聲明不會返回任何內容。

相關問題