-1

我遇到了转换错误,我需要一些帮助来克服。

目前,我正在尝试截取我的桌面并将其存储在我可以传递的变量中。现在,这段代码看起来像这样:

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

但是,这样做我得到以下错误:

无法将类型“void”隐式转换为“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)

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

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

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

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

     if (OnUpdateStatus == null) return;

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

我已经没有关于如何克服这些错误的想法了。任何人都可以阐明一些新的见解,以便我可以摆脱它们吗?

4

3 回答 3

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
 }
于 2013-08-23T10:59:42.323 回答
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
于 2013-08-23T10:59:29.783 回答
1

您实际上需要从 CaptureImage 方法返回一个 Image。您的退货声明没有返回任何内容。

于 2013-08-23T11:01:51.503 回答