1

我做了某种逻辑,但它得到错误?

这是我的代码

private static void DrawText(String text, Font font, Color textColor, Color backColor)
{
    Image img = new Bitmap(640, 360);
    Graphics drawing = Graphics.FromImage(img);

    Color color = textColor;
    if (text.Length <= 80) {
        Rectangle displayRectangle =
            new Rectangle(new Point(20, 100), new Size(img.Width - 1, img.Height - 1));
    } else {
        Rectangle displayRectangle =
            new Rectangle(new Point(20, 80), new Size(img.Width - 1, img.Height - 1));
    }
    StringFormat format1 = new StringFormat(StringFormatFlags.NoClip);
    StringFormat format2 = new StringFormat(format1);

     // ERROR ON NEXT LINE
    drawing.DrawString(text, font, Brushes.Red, (RectangleF)displayRectangle, format2);

    drawing.Dispose();
    string fileName = "f.png";
    string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
    img.Save(path, System.Drawing.Imaging.ImageFormat.Png);

    img.Dispose();
}

错误是

错误1 当前上下文中不存在名称'displayRectangle' C:\Documents and Settings\admin\My Documents\Visual Studio 2008>\Projects\template\template\Form1.cs 119 69 模板

在这一行

drawing.DrawString(text, font, Brushes.Red, (RectangleF)displayRectangle, format2);

在 php 中逻辑是正确的,但是在 C# 中它得到错误?,如何在 C# 中执行此逻辑?

有什么帮助吗?(仍在学习 C#:D)

4

7 回答 7

12

Rectangle定义移到if else块上方,使其不会超出范围。

Rectangle displayRectangle;

if (text.Length <= 80)
{
    displayRectangle = new Rectangle(new Point(20, 100), new Size(img.Width  img.Height - 1));
}
else
{
    displayRectangle = new Rectangle(new Point(20, 80), new Size(img.Width -  img.Height - 1));
}
于 2012-12-28T21:43:01.893 回答
5

基本上,它不会那样工作:)

if在一个或一个else块中定义的变量,在{和之间}仅在该块中可见。这就是为什么您能够定义它两次,一次在if分支中,一次在else分支中。

所以解决方法是在分支前声明变量,然后在 if/else 分支中设置。

Rectangle displayRectangle; //declaration

if (text.Length <= 80)
{
  //setting
  displayRectangle = new Rectangle(new Point(20, 100), new Size(img.Width - 1, img.Height - 1));
}
else
{
  //setting
  displayRectangle = new Rectangle(new Point(20, 80), new Size(img.Width - 1, img.Height - 1));
}
....
//usage
drawing.DrawString(text, font, Brushes.Red, (RectangleF)displayRectangle, format2);

此外,C# 编译器足够聪明,可以检测到每个分支中都设置了变量,所以当它到达它的使用代码时,它肯定会被设置。但是,如果不是在每个分支中都设置它,那将是一个编译错误,例如

if (text.Length <= 80)
{
  displayRectangle = ...;
}
else if (text.Length > 40)
{

  displayRectangle = ...;
}
于 2012-12-28T21:43:11.050 回答
4

displayRectangle您可以在 if/else 逻辑之外定义变量:

Rectangle displayRectangle;
if (text.Length <= 80)
{
    displayRectangle = new Rectangle(new Point(20, 100), new Size(img.Width - 1, img.Height - 1));
}
else
{
    displayRectangle = new Rectangle(new Point(20, 80), new Size(img.Width - 1, img.Height - 1));
}

现在这个变量将被外部范围所知。

于 2012-12-28T21:43:17.667 回答
3

您不能使用在当前范围之外声明的变量。您已displayRectangle在 内部声明if-clause,因此它在外部“不可见”。

而是在外面声明它并在里面初始化它:

Rectangle displayRectangle = Rectangle.Empty;
if (text.Length <= 80)
{
     displayRectangle = new Rectangle(new Point(20, 100), new Size(img.Width - 1, img.Height - 1));
}
else
{
    displayRectangle = new Rectangle(new Point(20, 80), new Size(img.Width - 1, img.Height - 1));
}

8.5.1 局部变量声明

在 local-variable-declaration 中声明的局部变量的范围是发生声明的块。在局部变量的局部变量声明符之前的文本位置引用局部变量是错误的。在局部变量的范围内,声明另一个同名的局部变量或常量是编译时错误。

于 2012-12-28T21:43:53.523 回答
1

将变量声明移到块外:

    Rectangle displayRectangle;
    if (text.Length <= 80)
    {
        displayRectangle = new Rectangle(new Point(20, 100), new Size(img.Width - 1, img.Height - 1));
    }
    else
    {
        displayRectangle = new Rectangle(new Point(20, 80), new Size(img.Width - 1, img.Height - 1));
    }

问题是代码中 displayRectangle 的范围。一旦块执行完毕,displayRectangle 就被完全遗忘,因为它是在块内声明的。

于 2012-12-28T21:45:15.760 回答
1

我认为这是因为您的初始化。

它应该是这样的:

 private static void DrawText(String text, Font font, Color textColor, Color backColor)
 {
    Rectangle displayRectangle; // YOU MUST DECLARE IT HERE OR OUTSIDE THE FUNCTION,
                                // but never inside an "if" statement.
    Image img = new Bitmap(640, 360);
    Graphics drawing = Graphics.FromImage(img);

    Color color = textColor;
    if (text.Length <= 80) {
        displayRectangle =
           new Rectangle(new Point(20, 100), new Size(img.Width - 1, img.Height - 1));
    } else {
        displayRectangle =
           new Rectangle(new Point(20, 80), new Size(img.Width - 1, img.Height - 1));
    }
    StringFormat format1 = new StringFormat(StringFormatFlags.NoClip);
    StringFormat format2 = new StringFormat(format1);

    // ERROR ON NEXT LINE
    drawing.DrawString(text, font, Brushes.Red, (RectangleF)displayRectangle, format2);

    drawing.Dispose();
    string fileName = "f.png";
    string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
    img.Save(path, System.Drawing.Imaging.ImageFormat.Png);

    img.Dispose();
}
于 2012-12-28T21:47:11.313 回答
1

如果你真的想要简洁,你可以用下面这行代码替换整个 IF/ELSE 块:

var displayRectangle = new Rectangle(20, (text.Length > 80 ? 80 : 100), img.Width - 1, img.Height - 1);

于 2012-12-28T22:28:11.070 回答