有人知道在C#中的方法之间共享字符串的方法吗?
这是我需要获取字符串的代码片段:
private void timer1_Tick(object sender, EventArgs e)
{
// The screenshot will be stored in this bitmap.
Bitmap capture = new Bitmap(screenBounds.Width, screenBounds.Height);
// The code below takes the screenshot and
// saves it in "capture" bitmap.
g = Graphics.FromImage(capture);
g.CopyFromScreen(Point.Empty, Point.Empty, screenBounds);
// This code assigns the screenshot
// to the Picturebox so that we can view it
pictureBox1.Image = capture;
}
在这里需要获取'capture'字符串:
Bitmap capture = new Bitmap(screenBounds.Width, screenBounds.Height);
并从此方法中“捕获”:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (paint)
{
using (Graphics g = Graphics.FromImage(!!!Put Capture in Here!!!))
{
color = new SolidBrush(Color.Black);
g.FillEllipse(color, e.X, e.Y, 5, 5);
}
}
}
在这里:
using (Graphics g = Graphics.FromImage(!!!Put Capture in Here!!!))
我希望有人可以提供帮助!
PS:如果你不了解整件事,我14岁,来自荷兰,所以我不是最好的英国作家: - )。答案 0 :(得分:3)
您正在查看变量的范围。
您的变量capture
是在方法级别定义的,因此只能使用该方法。
您可以在类级别(方法之外)定义变量,并且类中的所有方法都可以访问它。
Bitmap capture;
private void timer1_Tick(object sender, EventArgs e)
{
// The screenshot will be stored in this bitmap.
capture = new Bitmap(screenBounds.Width, screenBounds.Height);
// The code below takes the screenshot and
// saves it in "capture" bitmap.
g = Graphics.FromImage(capture);
g.CopyFromScreen(Point.Empty, Point.Empty, screenBounds);
// This code assigns the screenshot
// to the Picturebox so that we can view it
pictureBox1.Image = capture;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (paint)
{
using (Graphics g = Graphics.FromImage(capture))
{
color = new SolidBrush(Color.Black);
g.FillEllipse(color, e.X, e.Y, 5, 5);
}
}
}