【问题标题】:The name "e" does not exist under the current context error in DrawText methodDrawText 方法中当前上下文错误下不存在名称“e”
【发布时间】:2014-01-08 15:11:41
【问题描述】:

我正在用我用 C# 编写的方法渲染一些位图。

我正在学习使用TextRenderer.DrawText() 方法在位图上绘制文本。 sn-p如下:

TextRenderer.DrawText(e.Graphics, "Regular Text", SystemFonts.DefaultFont, new Point(10, 10), SystemColors.ControlText);

但是,在e.Graphics 下,我收到The name "e" does not exist under the current context 的错误。

我想知道可能是什么问题?

我的 Render 方法中的代码 sn-p,使用 Rhino-Common 库:

    protected override void Render(Grasshopper.GUI.Canvas.GH_Canvas canvas, Graphics graphics, Grasshopper.GUI.Canvas.GH_CanvasChannel channel) {
        base.Render(canvas, graphics, channel);

        if (channel == Grasshopper.GUI.Canvas.GH_CanvasChannel.Wires) {
            var comp = Owner as KT_HeatmapComponent;
            if (comp == null)
                return;

            List<HeatMap> maps = comp.CachedHeatmaps;
            if (maps == null)
                return;

            if (maps.Count == 0)
                return;

            int x = Convert.ToInt32(Bounds.X + Bounds.Width / 2);
            int y = Convert.ToInt32(Bounds.Bottom + 10);

            for (int i = 0; i < maps.Count; i++) {
                Bitmap image = maps[i].Image;
                if (image == null)
                    continue;

                Rectangle mapBounds = new Rectangle(x, y, maps[i].Width, maps[i].Height);
                mapBounds.X -= mapBounds.Width / 2;

                Rectangle edgeBounds = mapBounds;
                edgeBounds.Inflate(4, 4);

                GH_Capsule capsule = GH_Capsule.CreateCapsule(edgeBounds, GH_Palette.Normal);
                capsule.Render(graphics, Selected, false, false);
                capsule.Dispose();

                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
                graphics.DrawImage(image, mapBounds);
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Default;
                graphics.DrawRectangle(Pens.Black, mapBounds);
                TextRenderer.DrawText(e.Graphics, "Regular Text", SystemFonts.DefaultFont, new Point(10, 10), SystemColors.ControlText);

                y = edgeBounds.Bottom - (mapBounds.Height) - 4;
            }
        }
    }

上面的代码 sn-p 将热图渲染到 Grasshopper 画布上,如下所示:

但是,我有兴趣在其上添加标题和 x/y 轴文本。

【问题讨论】:

  • 那么您期望 e 来自哪里?我们这里没有上下文 - 这是在事件处理程序中吗?还有什么?
  • 可能 sn-p 来自一个绘制处理程序,并传递了一个名为 e 的事件对象。
  • 我刚刚了解到这是一个事件处理程序 arg。不,这不是一个事件处理程序,所以我可能使用了错误的方法将文本绘制到我的位图上。我会在我的编辑中更具体一点。
  • 已添加。谢谢大家的帮助!

标签: c# bitmap


【解决方案1】:

您发布的代码看起来像是设计在Paint 事件处理程序中(因为e 是事件处理程序的适当类型EventArgs 的惯用变量名,而Graphics 是一个PaintEventArgs 的属性)。

由于您已经被传递了一个Graphics 对象,您应该能够将您的代码sn-p 中的e.Graphics 替换为传递给您的Render 函数的graphics 参数。您可以将此行添加到该函数:

TextRenderer.DrawText(graphics, "Regular Text", SystemFonts.DefaultFont, new Point(10, 10), SystemColors.ControlText);

【讨论】:

  • graphics 已经作为参数传入。无需使用CreateGraphics()
  • 如果上面已经声明了graphics,我该怎么办?
  • @LarsTech:这是在添加功能体的问题编辑之前。最初发布的只是TextRenderer 一行。
  • 不幸的是,这似乎不起作用:puu.sh/6dQl0/b35ab90cd3.png 我也尝试过其他渐变方案,例如:puu.sh/6dQm0/9bf93f9bda.png,所以文本的颜色应该不是问题。
  • @theGreenCabbage:尝试将其添加到函数的末尾,以确保文本上没有任何内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-01
  • 2010-12-17
  • 2019-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多