在 C# 中的方法之间共享字符串

本文关键字:之间 共享 字符串 方法 | 更新日期: 2023-09-27 18:33:15

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

在 C# 中的方法之间共享字符串

您正在查看变量的范围

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