Friday, 23 August 2013

Converting a type void to 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'
So, I tried to type cast the capture.CaptureImage and it generated the
same error. 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:
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:
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.
This error points to the following chunk of code with the second if
statement being the culprit:
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?

No comments:

Post a Comment