简体   繁体   English

将类型转换为System.Drawing.Image

[英]Converting a type void to System.Drawing.Image

I'm having a conversion error I would like some help in overcoming. 我遇到转换错误,需要克服一些帮助。

At the moment I am trying to take a screen capture of my desktop and store it in a variable I can pass around. 目前,我正在尝试对桌面进行屏幕截图并将其存储在我可以传递的变量中。 Right now, this code looks like this: 现在,该代码如下所示:

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

However, by doing that I get the following error: 但是,这样做会出现以下错误:

Cannot implicitly convert type 'void' to 'System.Drawing.Image' 无法将类型“ void”隐式转换为“ System.Drawing.Image”

So, I tried to type cast the capture.CaptureImage and it generated the same error. 因此,我尝试键入强制转换capture.CaptureImage,并生成了相同的错误。 The line I wrote was thus: 因此,我写的那行是:

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

My CaptureImage method is the following: 我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);

        }

So, seeing that I have set the method to be void, I changed it so that it would require an image like so: 因此,看到我将方法设置为void,我对其进行了更改,使其需要如下图像:

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

But then it generated the following error: 但随后生成以下错误:

An object of a type convertible to 'System.Drawing.Image' is required. 需要可转换为“ System.Drawing.Image”类型的对象。

This error points to the following chunk of code with the second if statement being the culprit: 此错误指向以下代码块,第二个if语句是罪魁祸首:

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

     if (OnUpdateStatus == null) return;

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

I've ran out of ideas on how to beat these errors. 我没有关于如何克服这些错误的想法。 Can anyone please shed some light / new insight so that I can get rid of them? 任何人都可以阐明一些新见解,以便我摆脱它们吗?

Your CaptureImage requires an Image returned but you return void/nothing in the if (OnUpdateStatus == null) . 您的CaptureImage要求返回一个Image但是在if (OnUpdateStatus == null)返回void/nothing if (OnUpdateStatus == null) Even you return some image there, you still have to return the image outside that if block, otherwise it may complain that Not all code paths return a value . 即使您在那里返回了一些图像,也仍然必须在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
 }

because your method 因为你的方法

public Image CaptureImage( 

expects a return type of image that you are not returning. 期望返回不返回的图像类型。 There are 2 points of return in your code. 有在你的代码返回2分。

Point 1: 第一点:

if (OnUpdateStatus == null) return;

gives error because as your return type needs to be image and you are not returning that so you should do it like 给出错误,因为您的返回类型必须是图像,而您没有返回该图像,因此您应该这样做

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

and before exiting the method you need to return image as well 在退出该方法之前,您还需要返回图像

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

You need to actually return an Image from your CaptureImage method. 您实际上需要从CaptureImage方法返回一个Image。 Your return statement isn't returning anything. 您的return语句不返回任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM