【发布时间】:2013-08-21 15:46:25
【问题描述】:
我是在 Winforms 中使用事件处理程序的新手。
我要做的是使用鼠标对我的桌面进行屏幕截图,以指定要捕获的区域,然后将此图像显示在图片中。有人建议我在另一篇文章中使用事件处理程序来执行此操作,但我在尝试使其正常工作时遇到了一个真正的问题。有人可以看看我做了什么并告诉我我做错了什么吗?
如果做不到这一点,请指出我正确的方向来解决这个问题?
首先,我的屏幕截图代码:
class ScreenCapture
{
public delegate void StatusUpdateHandler(object sender, ProgressEventArgs e);
public static event StatusUpdateHandler OnUpdateStatus;
public static bool saveToClipboard = true;
public static void CaptureImage(bool showCursor, Size curSize, Point curPos, Point SourcePoint, Point DestinationPoint, Rectangle SelectionRectangle, string FilePath, string extension)
{
using (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);
// this is where the screen capture is stored.
// it is this image I want pass to the image box
}
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 { get; private set; }
public ProgressEventArgs( Image img)
{
Image = img;
}
}
以及我的 form1 类中的代码:
public partial class ControlPanel : Form
{
public EventHandler<ScreenCapture> capture;
private ScreenCapture _screenCap;
public ControlPanel()
{
InitializeComponent();
_screenCap = new ScreenCapture();
_screenCap.OnUpdateStatus += new ScreenCapture.StatusUpdateHandler(SetImage);
}
private void SetImage(object sender, ProgressEventArgs e)
{
// the image box I want the image to be passed to
// imagePreview.Image = e.OnUpdateStatus;
SetImage(e.Image);
}
private void btn_CaptureArea_Click(object sender, EventArgs e)
{
this.Hide();
Form1 form1 = new Form1();
form1.InstanceRef = this;
form1.Show();
}
}
我以前从未使用过这样的事件处理程序,所以我有点不知所措。
编辑
抱歉,忘记为我的问题添加更多细节。尽管主要错误仍然是我的程序不再像以前使用此新代码那样工作。我仍然希望捕获的图像显示在我的 form1 中的图片框中。
目前我的事件处理代码有 8 个错误。在以下代码段中:
public class ProgressEventArgs : EventArgs
{
public readonly Image { get; private set; }
public ProgressEventArgs( Image img)
{
Image = img;
}
}
我的编译器不喜欢 get 或 set。正如我的编译器声称的那样
无效的令牌';'在类、结构或接口成员声明中
然后,在下面的行中,ProgressEventArgs 得到以下错误:
预期的类、委托、枚举、接口或结构 但我的想法是它已经在课堂上。所以我真的被这个错误弄糊涂了。
【问题讨论】:
-
Tell me what I've done wrong?但是我们首先需要知道您的问题是什么(详细)? -
更新代码以反映我的更改。
-
此类错误与您的语法有关,这意味着您缺少一些
punctuations例如}, ;, {, ...您应该尝试将提示集中在您的类的开头{以查看其他@ 987654328@在正确的位置被高亮,这样你可以找到如果你错过了一些括号......并自己修复它。
标签: c# winforms event-handling