C# 图形位图中文本坐标的放置位置

the*_*age 3 c# canvas bitmap rhino-commons grasshopper

我编写了一个 C# 渲染方法,将热图渲染到 Grasshopper 画布上。Grasshopper 是一个 Rhino 插件,支持简单的 GUI 编程界面。

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);
                    //Rectangle mapBounds = new Rectangle(x, y, maps[i].Width * 10, maps[i].Height * 10);
                    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);

                    y = edgeBounds.Bottom - (mapBounds.Height) - 4;
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

目前,此渲染方法在画布上绘制如下图像:

在此输入图像描述

话虽如此,我想在顶部放置一些标题文本,并为 X 轴和 Y 轴添加标签,就像标准热图一样。不过我对这个graphics组件的了解还太有限,还请各位大佬帮忙。

我做了一些研究,似乎该drawText()方法可以实现我想要的功能:c# write text on bitmap

但我不确定在哪里指定坐标,同时在显示的图表顶部留出一些空间来放置标题文本。

tec*_*hno 5

GDI+使用的坐标系从左上角开始,即(0,0) 右下角(fullimagewidth,fullimageheight)

在此输入图像描述

因此,如果您需要在图像的左上角绘制,请使用

//Position
PointF drawPoint = new PointF(0F, 0F);
// Draw string to screen.
e.Graphics.DrawString("hey", drawFont, drawBrush, drawPoint);
Run Code Online (Sandbox Code Playgroud)