我在C#中编写的方法中渲染了一些Bitmaps。
我正在学习使用TextRenderer.DrawText()
方法在位图上绘制文本。摘录如下:
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方法的代码片段,使用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;
}
}
}
上面的代码片段将热图呈现到Grasshopper画布上,如下所示:
但是,我有兴趣在其上添加标题和x / y轴文本。
答案 0 :(得分:3)
您发布的代码看起来像设计在Paint
事件处理程序中(因为e
是事件处理程序的适当类型EventArgs
的惯用变量名称, Graphics
是PaintEventArgs
)的属性。
由于您已经传递了Graphics
个对象,因此您应该能够在代码段中替换e.Graphics
,并将graphics
参数传递到您的Render
功能。您可以将此行添加到该函数:
TextRenderer.DrawText(graphics, "Regular Text", SystemFonts.DefaultFont, new Point(10, 10), SystemColors.ControlText);