将类型void转换为System.Drawing.Image

时间:2013-08-23 10:57:27

标签: c# winforms image

我遇到转换错误,我希望能帮助克服。

目前我正在尝试对我的桌面进行屏幕截图并将其存储在我可以传递的变量中。现在,这段代码如下所示:

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

但是,通过这样做,我收到以下错误:

  

无法将类型'void'隐式转换为'System.Drawing.Image'

所以,我尝试输入cast.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)

但后来它产生了以下错误:

  

需要可转换为'System.Drawing.Image'的类型的对象。

此错误指向以下代码块,第二个if语句是罪魁祸首:

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

     if (OnUpdateStatus == null) return;

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

我已经没有关于如何击败这些错误的想法。任何人都可以请一些亮点/新见解,以便我可以摆脱它们吗?

3 个答案:

答案 0 :(得分:3)

您的CaptureImage需要Image返回,但您会在void/nothing中返回if (OnUpdateStatus == null)。即使你在那里返回一些图像,你仍然必须将图像返回到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 :(得分: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 :(得分:1)

您需要从CaptureImage方法实际返回一个Image。你的退货声明没有退货。