我遇到了转换错误,我需要一些帮助来克服。
目前,我正在尝试截取我的桌面并将其存储在我可以传递的变量中。现在,这段代码看起来像这样:
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);
}
我已经没有关于如何克服这些错误的想法了。任何人都可以阐明一些新的见解,以便我可以摆脱它们吗?