N0xus
N0xus

Reputation: 2724

Event handlers in C#

I'm really new to using event handlers in C# in winforms and at the moment I have the following error:

Error 1 The type 'DotFlickScreenCapture.ScreenCapture' cannot be used as type parameter 'TEventArgs' in the generic type or method 'System.EventHandler'. There is no implicit reference conversion from 'DotFlickScreenCapture.ScreenCapture' to 'System.EventArgs'.

I've tried searching for a way to beat this error but so far, my google searches have not turned up anything.

The line this error points to is this one:

  public EventHandler<ScreenCapture> capture;

And from what I can tell, this class:

public class ScreenCapture
{
    public delegate void StatusUpdateHandler(object sender, ProgressEventArgs e);
    public event StatusUpdateHandler OnUpdateStatus;

    public bool saveToClipboard = true;

    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;
            Clipboard.SetImage(img);

            if (OnUpdateStatus == null) return;

            ProgressEventArgs args = new ProgressEventArgs(img);
            OnUpdateStatus(this, args);
        }
        else
        {
            switch (extension)
            {
                case ".bmp":
                    bitmap.Save(FilePath, ImageFormat.Bmp);
                    break;
                case ".jpg":
                    bitmap.Save(FilePath, ImageFormat.Jpeg);
                    break;
                case ".gif":
                    bitmap.Save(FilePath, ImageFormat.Gif);
                    break;
                case ".tiff":
                    bitmap.Save(FilePath, ImageFormat.Tiff);
                    break;
                case ".png":
                    bitmap.Save(FilePath, ImageFormat.Png);
                    break;
                default:
                    bitmap.Save(FilePath, ImageFormat.Jpeg);
                    break;
            }
        }
    }
}


public class ProgressEventArgs : EventArgs
{
    public Image CapturedImage { get; private set; }
    public ProgressEventArgs(Image img)
    {
        CapturedImage = img;
    }
}

Has anyone ever experienced this error before? Is so, how can I overcome it?

Upvotes: 0

Views: 1570

Answers (1)

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56727

The ScreenCapture class must derive from the EventArgs class to be used the way you want.

public class ScreenCapture : EventArgs

And then (to avoid misunderstandings) it should be named ScreenCaptureEventArgs. Thinking of it, it would be easier to just create a class ScreenCaptureEventArgs that derives from EventArgs and contains a property ScreenCapture that's an instance of the class you already have.

Like that:

public class ScreenCaptureEventArgs : EventArgs
{
    public ScreenCaptureEventArgs(ScreenCapture c)
    {
        Capture = c;
    }

    public ScreenCapture Capture { get; private set; }
}

public event EventHandler<ScreenCaptureEventArgs> ScreenCaptured;

Upvotes: 6

Related Questions