1

有人知道在 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;

    }

在这里需要获取“捕获”字符串:

        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 岁,来自荷兰,所以我不是最好的英语作家 :-)。

4

1 回答 1

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);
        }
    }

}
于 2013-09-27T18:41:22.330 回答