【发布时间】: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。不,这不是一个事件处理程序,所以我可能使用了错误的方法将文本绘制到我的位图上。我会在我的编辑中更具体一点。
-
已添加。谢谢大家的帮助!